K
K
Kawai4ik2020-02-14 13:51:26
API
Kawai4ik, 2020-02-14 13:51:26

How to load data for TableView?

Tell me how to properly organize data subloading for TableView so that there are no lags and it is "correct".
There is an API from which I receive data (I have my own API, I can edit it as needed). Let's say I pull out the 20 nearest objects and give them to the application. They are displayed.
Further, the user, after viewing all 20, tries to scroll the table below 20 (we only have 20 objects), more than 20 is not loaded, but after 20 entries, another 20 should be made, and so on.
Tell me how to properly organize and where to load data for TableView - to solve such a problem?

Here is my source code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Jsonnn : MonoBehaviour
{
    public RectTransform prefab;
    public Text countText;
    public RectTransform content;
    public GameObject ItemPrefab;

    public Sprite icon;
    public int cells;

    public void UpdateItems()
    {
        float height = prefab.rect.height;

        if (height < 100 * cells)
        {
            height = 100 * cells;
        }
        string url = "http://62.113.112.195:8000/page/0";
        WWW www = new WWW(url);
        StartCoroutine(GetItems(www, results => OnReceivedModels(results)));

        content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
        content.SetPositionAndRotation(new Vector3(0, -height / 2, 0), Quaternion.Euler(0, 0, 0));

        for (int i = 0; i < cells; i++)
        {
            GameObject cell = Instantiate(ItemPrefab);
            cell.transform.SetParent(content.transform, false);      
        }
    }
    void OnReceivedModels(TestItemModel[] models)
    {
        foreach (Transform child in content)
        {
            Destroy(child.gameObject);
        }
        foreach (var model in models)
        {
            var instance = GameObject.Instantiate(prefab.gameObject) as GameObject;
            instance.transform.SetParent(content, false);
            InitializeItemView(instance, model);
        }
    }
    void InitializeItemView(GameObject viewGameObject, TestItemModel model)
    {
        TestItemView view = new TestItemView(viewGameObject.transform);
        view.titleText.text = model.title;
        view.Time.GetComponentInChildren<Text>().text = model.time;
        view.Day.GetComponentInChildren<Text>().text = model.day;
        view.Year.GetComponentInChildren<Text>().text = model.year;
        view.Dayweek.GetComponentInChildren<Text>().text = model.dayweek;
        {
                Debug.Log(view.titleText.text + " is clicked!");
            }   
    }
    IEnumerator GetItems(WWW www, System.Action<TestItemModel[]> callback)
    {
        yield return www;

        if (www.error == null)
        {
            TestItemModel[] mList = JsonHelper.getJsonArray<TestItemModel>(www.text);
            Debug.Log("WWW Success: " + www.text);
            callback(mList);
        }
        else
        {
            TestItemModel[] errList = new TestItemModel[1];
            errList[0] = new TestItemModel();
            errList[0].title = www.error;
            
            Debug.Log("WWW Error: " + www.error);
            callback(errList);
        }
    }
    public class TestItemView
    {
        public Text titleText;
        public Text Time;
        public Text Day;
        public Text Dayweek;
        public Text Year;

        public TestItemView(Transform rootView)
        {
            titleText = rootView.Find("titleText").GetComponent<Text>();
            Time = rootView.Find("Time").GetComponent<Text>();
            Day = rootView.Find("Day").GetComponent<Text>();
            Dayweek = rootView.Find("Dayweek").GetComponent<Text>();
            Year = rootView.Find("Year").GetComponent<Text>();
        }
    }
    [System.Serializable]
    public class TestItemModel
    {
        public string title;
        public string time;
        public string day;
        public string dayweek;
        public string year;
    }
    public class JsonHelper
    {
        public static T[] getJsonArray<T>(string json)
        {
            string newJson = "{ \"array\": " + json + "}";
            Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
            return wrapper.array;
        }
        public static string arrayToJson<T>(T[] array)
        {
            Wrapper<T> wrapper = new Wrapper<T>();
            wrapper.array = array;
            return JsonUtility.ToJson(wrapper);
        }
        [System.Serializable]
        private class Wrapper<T>
        {
            public T[] array;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question