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

这里是我尝试创建单元格的代码(我正在使用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;
}发布于 2016-12-20 06:51:05
首先实例化单元变量,然后在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;
}发布于 2016-12-20 07:22:37
您的问题是foreach循环的经典问题。基本上,系统用集合中的新索引项覆盖当前引用。因此,尽管您将值存储在局部变量中,但条目引用会被覆盖,然后所有值最终都指向相同的条目对象。
解决方案是创建一个本地条目引用:
foreach (GameSparks.Api.Responses.LeaderboardDataResponse._LeaderboardData entry in resp.Data) {
var localEntry = entry; // New line
int rank = (int)localEntry.Rank; // entry is replaced with local
string playerName = localEntry.UserName;
cell.name = playerName;
string score = localEntry.JSONData ["SCORE"].ToString (); line
cell.SetScore(score);
Debug.Log ("Rank:" + rank + " Name:" + playerName + " \n Score:" + score);
}编辑:我看到您正在返回循环外的单元格,所以即使循环修复了一个问题,但在其他地方有另一个问题。
在每个循环中,您将填充单元格数据。但是在每个循环中,您都要重写单元格数据。最后,返回最后一个值。您可以返回一个单元格数组并使用该数组创建项目,或者将一个项数组传递给该方法,因此对于循环的每次迭代,还可以设置匹配项的值(使用索引)。
https://stackoverflow.com/questions/41236305
复制相似问题