美文网首页
BUG日志:indexPath.row不从0开始

BUG日志:indexPath.row不从0开始

作者: 213dfddbef5e | 来源:发表于2017-03-03 15:28 被阅读105次

原代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SClockCell"];
    SClock *sclock = (self.clocklist_Array)[indexPath.row];
    // 这里出错了
    UILabel * label= (UILabel *)[self.view viewWithTag:500];
    label.text = sclock.clockTime;
    
    return cell;
}

原因:

UILabel * label= (UILabel *)[self.view viewWithTag:500];

cell 复用时都是用 Tag:500 的 label 显示,这样就错了。因为每多一个 cell,就多一个 tag 为 500 的 label 叠在原地,而 label 上显示的内容是最上层那个 label 的内容,就会出现 “indexPath.row 不从 0 开始”这种情况。

解决办法:

新建 cell.h 和 cell.m,在其中拉进 StoryBorad 的 label 控件,生成属性(@property)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FirstPageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FirstPageCell"];
    SClock *sclock = (self.clocklist_Array)[indexPath.row];
    // 注意这里
    cell.FPLabel.text = sclock.clockTime;

    return cell;
}

相关文章

网友评论

      本文标题:BUG日志:indexPath.row不从0开始

      本文链接:https://www.haomeiwen.com/subject/rscrgttx.html