简体   繁体   English

TableViewCell不在第一次显示数据

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

I have two UILabels on my UITableViewCell . 我的UITableViewCell上有两个UILabels

I am using storyboard and I have these labels in custom cell as shown below: 我正在使用情节提要,并且在自定义单元格中具有这些标签,如下所示:

在此处输入图片说明

My cellForRowAtIndex method: 我的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;

}

My Problem is when First time I open My controller the data is shown from second Index and in last I see the The custom label name as shown below: 我的问题是,第一次打开控制器时,数据从第二个索引显示,最后我看到自定义标签名称,如下所示:

在此处输入图片说明

And when I scroll then only data is updated and In random order I am not able to get the data in right order. 而且,当我滚动时,仅更新数据,并且不能以随机顺序获取正确顺序的数据。

When I debug When The indexPath is 0 aim getting nil in UILabel but when It becomes 1 I am getting correct value. 当我调试时当indexPath为0时,目标是UILabel为零,但是当它变为1时,我将获得正确的值。

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

Some one told me using cell.ContentView in place of self.view will solve my problem. 有人告诉我使用cell.ContentView代替self.view将解决我的问题。 But I am confused How can this be. 但是我很困惑。

As i would recommend that the way you are following is not correct. 正如我所建议的那样,您遵循的方式是不正确的。 As per MVC, your view classes and your controller classes should be different if you are making something custom. 根据MVC,如果要进行自定义,则视图类和控制器类应该有所不同。 In your case you can make a UITableViewCell class and than use its object to initialise your cell. 在您的情况下,您可以创建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";

In above code CommentsTableViewCell is a custom class for UITableViewCell. 在上面的代码中,CommentsTableViewCell是UITableViewCell的自定义类。 Hope you will find it helpful. 希望对您有所帮助。

Try changing these lines, 尝试更改这些行,

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

with

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

If it is not working, let me know. 如果它不起作用,请告诉我。

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

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