繁体   English   中英

TableViewCell不在第一次显示数据

[英]TableViewCell doesn't show data on first time

我的UITableViewCell上有两个UILabels

我正在使用情节提要,并且在自定义单元格中具有这些标签,如下所示:

在此处输入图片说明

我的cellForRowAtIndex方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    static NSString *simpleTableIdentifier = @"cell";

NSLog(@"index = %ld",indexPath.row);  //This logs correct 0,1,2 ..and so on

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
UILabel *lblCellTitle = (UILabel*)[self.view viewWithTag:1];
UILabel *lblCellDetail = (UILabel*)[self.view viewWithTag:2];

lblCellTitle.text = [arrtableDataHeading objectAtIndex:indexPath.row]; //this array contain valid values
lblCellDetail.text = [filterdInfo objectAtIndex:indexPath.row]; //this array contain valid values

return cell;

}

我的问题是,第一次打开控制器时,数据从第二个索引显示,最后我看到自定义标签名称,如下所示:

在此处输入图片说明

而且,当我滚动时,仅更新数据,并且不能以随机顺序获取正确顺序的数据。

当我调试时当indexPath为0时,目标是UILabel为零,但是当它变为1时,我将获得正确的值。

在此处输入图片说明 在此处输入图片说明

有人告诉我使用cell.ContentView代替self.view将解决我的问题。 但是我很困惑。

正如我所建议的那样,您遵循的方式是不正确的。 根据MVC,如果要进行自定义,则视图类和控制器类应该有所不同。 在您的情况下,您可以创建UITableViewCell类,然后使用其对象初始化单元格。

CommentsTableViewCell *cell = (CommentsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"comments cell"];
    cell.lblName.text = @"String data";
    NSString *dateString = @"String data";
    cell.lblCommentTime.text = @"String data";
    cell.imgDp.image = [UIImage imageNamed: @"String data"];
    cell.lblCommentDuration.text = @"String data";

在上面的代码中,CommentsTableViewCell是UITableViewCell的自定义类。 希望对您有所帮助。

尝试更改这些行,

UILabel *lblCellTitle = (UILabel*)[self.view viewWithTag:1];
UILabel *lblCellDetail = (UILabel*)[self.view viewWithTag:2];

UILabel *lblCellTitle = (UILabel*)[cell.contentView viewWithTag:1];
UILabel *lblCellDetail = (UILabel*)[cell.contentView viewWithTag:2];

如果它不起作用,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM