gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:16:23 33 4
gpt4 key购买 nike

我正在使用 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com