从 nib 加载 UITableViewCell 时遇到问题

发布于 2024-11-30 20:29:12 字数 1393 浏览 3 评论 0原文

我在从笔尖加载 UITableViewCell 时遇到问题。我使用了苹果文档,但我不知道哪里出错了。通过IB创建的UITableViewCell包含5个UIlable。笔尖加载完美,但问题是当我滚动标签文本时会自动更改为不同的。 以下是我的索引路径行单元格的代码。请让我知道我要去哪里。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowNo = [indexPath row];
NSLog(@"Row No:%d",rowNo);
Transfer *tempTransferRecord =(Transfer*)[self.transferInformation objectAtIndex:rowNo];
seasonYear.text=tempTransferRecord.seasonYear;
longName.text=tempTransferRecord.longName;
transactionDate.text=tempTransferRecord.transactionDate;
toTeam.text=tempTransferRecord.toTeam;
fromTeam.text=tempTransferRecord.fromTeam;
/*
static NSString *MyIdentifier = @"MyIdentifier2";    
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier] ;

if (cell == nil) 
{
    [[NSBundle mainBundle] loadNibNamed:@"PlayerTransfers" owner:self options:nil];
    cell=transfersInfoCell;
    self.transfersInfoCell=nil;
}

 */


static NSString *CellIdentifier = @"MyIdentifier2";
static NSString *CellNib = @"PlayerTransfers";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
    cell = (UITableViewCell *)[nib objectAtIndex:0];
}

// perform additional custom work...

return cell;

}

I am having trouble wiht UITableViewCell loaded from nib. I used apple documentation to that but I do't know where I am going wrong. UITableViewCell created through IB contains 5 UIlable. Nib are loaded perfectly but the problem is when I scroll text of the label changes automatically to different.
Following is the my code for cell for row at indexpath. Please let me know where I am going worng.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowNo = [indexPath row];
NSLog(@"Row No:%d",rowNo);
Transfer *tempTransferRecord =(Transfer*)[self.transferInformation objectAtIndex:rowNo];
seasonYear.text=tempTransferRecord.seasonYear;
longName.text=tempTransferRecord.longName;
transactionDate.text=tempTransferRecord.transactionDate;
toTeam.text=tempTransferRecord.toTeam;
fromTeam.text=tempTransferRecord.fromTeam;
/*
static NSString *MyIdentifier = @"MyIdentifier2";    
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier] ;

if (cell == nil) 
{
    [[NSBundle mainBundle] loadNibNamed:@"PlayerTransfers" owner:self options:nil];
    cell=transfersInfoCell;
    self.transfersInfoCell=nil;
}

 */


static NSString *CellIdentifier = @"MyIdentifier2";
static NSString *CellNib = @"PlayerTransfers";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
    cell = (UITableViewCell *)[nib objectAtIndex:0];
}

// perform additional custom work...

return cell;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一紙繁鸢 2024-12-07 20:29:12

尝试将单元格加载代码移至 cellForRowAtIndexPath 的顶部,然后将 rowNo 和值设置移至其后。单元格更改内容必定意味着您要么在首次设置数据源时与滚动时更改数据源中的内容,要么在错误的时间点设置值(即在第 5 行设置值,然后加载第 6 行)从笔尖,或在一行上设置值,然后为其加载笔尖,以便您设置的值被覆盖)。另外,我有点担心您设置的是局部变量,而不是 (PlayerTransfers*)cell.seasonYear.text = tempTransferRecord.seasonYear(UILabel *)[cell viewWithTag: 1].text = tempTransferRecord.seasonYear。您不应该在每个单元格内而不是在表视图类上设置值吗?这可以解释为什么值会变成不同的东西:您可以使用相同的变量来设置每个单元格。

Try moving your cell loading code to the top of cellForRowAtIndexPath, and the rowNo and value setting after that. Cells changing content must mean you're either changing what's in the data source between when you first set it and when you scroll, or you're setting values at the wrong point in time (i.e setting values on row 5 but then loading row 6 from a nib, or setting values on a row but then loading the nib for it afterwards so the values you set are overridden). Also, I'm a bit concerned that you set local variables rather than, say, (PlayerTransfers*)cell.seasonYear.text = tempTransferRecord.seasonYear or (UILabel *)[cell viewWithTag:1].text = tempTransferRecord.seasonYear. Shouldn't you be setting the values within each cell rather than on your table view class? That could explain why values are changing to something different: you may use the same variables to set every cell.

心不设防 2024-12-07 20:29:12

您是否在 Interface Builder 中设置了CellIdentifier

Have you set a CellIdentifier in Interface Builder?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文