原代码:
- (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;
}
网友评论