c# - 使用 For-Each 循环创建 TableView

标签 c# android ios unity-game-engine tableview

我正在使用 Unity3D 资源为我的应用程序创建一个 TableView。但是,当我尝试在我的 for-each 循环中从一个对象移动到另一个对象时,它只显示响应中的最后一个对象。这就是我的意思:

enter image description here

这是我尝试创建单元格的代码(仅供引用,我使用 GameSparks 作为后端服务):

//Will be called by the TableView to know how many rows are in this table
public int GetNumberOfRowsForTableView (TableView tableView)
{
    return 10;
}

//Will be called by the TableView to know what is the height of each row
public float GetHeightForRowInTableView (TableView tableView, int row)
{
    return (m_cellPrefab.transform as RectTransform).rect.height;
}

//Will be called by the TableView when a cell needs to be created for display
public TableViewCell GetCellForRowInTableView (TableView tableView, int row)
{
    LeaderboardCell cell = tableView.GetReusableCell (m_cellPrefab.reuseIdentifier) as LeaderboardCell;

    if (cell == null) {
        cell = (LeaderboardCell)GameObject.Instantiate (m_cellPrefab);
        new GameSparks.Api.Requests.LeaderboardDataRequest ().SetLeaderboardShortCode ("High_Score_Leaderboard").SetEntryCount (100).Send ((response) => {
            if (!response.HasErrors) {
                resp = response;
                Debug.Log ("Found Leaderboard Data...");

            } else {
                Debug.Log ("Error Retrieving Leaderboard Data...");
            }
        });
    }

    foreach (GameSparks.Api.Responses.LeaderboardDataResponse._LeaderboardData entry in resp.Data) {
        int rank = (int)entry.Rank;
        //model.Rank =rank;
        string playerName = entry.UserName;
        cell.name = playerName;
        string score = entry.JSONData ["SCORE"].ToString ();
        cell.SetScore(score);
        //string fbid = entry.ExternalIds.GetString("FB").ToString();
        //model.facebookId = fbid;
        Debug.Log ("Rank:" + rank + " Name:" + playerName + " \n Score:" + score);
    }
    return cell;
}

最佳答案

您首先实例化单元格变量,然后对您的 resp.Data 执行 for 循环。问题是,您只是遍历所有数据,并在每次迭代中设置名称和分数。每次迭代都会覆盖这些值,然后在循环结束时返回最后一个版本的单元格。 根据您在 GetCellForRowInTableView 方法上方的评论,您不应该真正在其中循环,因为应该为每个项目调用一次此方法。因此,你想要做的而不是循环是这样的:

public TableViewCell GetCellForRowInTableView (TableView tableView, int row)
{
    LeaderboardCell cell = tableView.GetReusableCell (m_cellPrefab.reuseIdentifier) as LeaderboardCell;

    if (cell == null) {
        cell = (LeaderboardCell)GameObject.Instantiate (m_cellPrefab);
        new GameSparks.Api.Requests.LeaderboardDataRequest ().SetLeaderboardShortCode ("High_Score_Leaderboard").SetEntryCount (100).Send ((response) => {
            if (!response.HasErrors) {
                resp = response;
                Debug.Log ("Found Leaderboard Data...");

            } else {
                Debug.Log ("Error Retrieving Leaderboard Data...");
            }
        });
    }

    var entry = resp.Data[row];
        int rank = (int)entry.Rank;
        //model.Rank =rank;
        string playerName = entry.UserName;
        cell.name = playerName;
        string score = entry.JSONData ["SCORE"].ToString ();
        cell.SetScore(score);
        //string fbid = entry.ExternalIds.GetString("FB").ToString();
        //model.facebookId = fbid;
        Debug.Log ("Rank:" + rank + " Name:" + playerName + " \n Score:" + score);

    return cell;
}

关于c# - 使用 For-Each 循环创建 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41236305/

相关文章:

java - 无法解决 "Parcelable encountered IOException writing serializable object"

objective-c - 两个目标 一个通用应用程序

ios - dequeueReusableCellWithIdentifier 一直返回 nil

c# - 如何在 Entity Framework 中具有映射到可为空的数据库列的值类型?

c# - mvc6中app、服务和中间件的区别

android - Android 中的 URL 错误

php - 如何让原生 Android 应用程序通过 Web 后端进行身份验证?

iphone - iPhone 应用程序中的最大线程数是多少?

c# - 我如何获得我与 Spring.NET IoC 的依赖关系的图形表示?

c# - 底层连接已关闭: An unexpected error occurred on a receive