0

我的UITableview. 滚动或按下开关后,有时会开始混淆单元格。所以你可以让一个单元格的文本显示在另一个单元格中。我阅读了很多关于 tableViews 的内容,但没有发现任何适合我的代码的内容。这里是:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];

}

    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = NSLocalizedString(@"Course:",@"Course Section");
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.detailTextLabel.text = entryTableCourseName;

        }

       else if (indexPath.row == 1) {
            cell.textLabel.text = @"Due Date:";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.detailTextLabel.text = entryTableDueDateString;            }
    }

    else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            //cell.textLabel.text = NSLocalizedString(@"Title", @"entryTableTaskTitle");
            [cell.contentView addSubview:nameField];

        }

        else if (indexPath.row == 1) {
            [cell.contentView addSubview:TaskView];
        }

    }


    else if (indexPath.section == 2) {

        if (entryTableGradeSwitch.on) {
            if (indexPath.row == 0) {
                cell.textLabel.text = NSLocalizedString(@"Grading", @"GradingCell");
                cell.accessoryView = entryTableGradeSwitch;
            }
            else if (indexPath.row == 1) {
                cell.textLabel.text = NSLocalizedString(@"Options", @"GradingCellOptions");
            }
        }
        else {
            cell.textLabel.text = NSLocalizedString(@"Grading", @"GradingCell");
            cell.accessoryView = entryTableGradeSwitch;
        }
    }

}

我觉得我在设置细胞时做错了什么。

4

4 回答 4

2

这一切都归功于单元格的可重用性。StackOverflow 上有很多关于此的帖子,我建议您搜索“UITableViewCell Reusability Problems”,您会遇到解决方案。

简而言之..这里发生的是,当您滚动时,单元格实质上会重新使用之前在该 indexPath 处使用的单元格,并且 cellForRowAtIndexPath 在该 indexpath 处分配一个不同的单元格。

解决方案 ?

  1. Use different reuse identifiers for each cell.
  2. Subclass your tableViewCell with the view or label that is getting messed up.
于 2012-06-06T17:21:15.457 回答
1

Make sure you implement

- (void) prepareForReuse

in your cell if you reuse them. In this routine clean up anything you added to the cell that you don't want to show up in another reuse. If you don't add any custom items just make sure when you populate a cell that you set everything even if it's empty.

于 2012-06-06T18:27:00.180 回答
0

Every time you do this:

 [cell.contentView addSubview:TaskView]

Or something like it, you are adding a new subview to the cell. As the cells get reused, you add more and more subviews.

From your code, it isn't even clear where nameView and taskView come from. They should be either added once when a cell is first created, and just configured thereafter, or they should be part of your cell as designed in the xib or storyboard.

于 2012-06-06T17:26:01.540 回答
0

Here's a super-simple fix for this, and you still get to keep your reuse-identifiers! Also no subclassing or anything needed.

It's simple: just make sure and set all of your labels and views. This means that if one cell isn't going to use the detail text label, still set it to @"". By doing this, you make sure that no leftover data from the content that used the cell before is still there.

Super simple, and works every time! Also way better resource wise than not reusing your cells.

于 2013-05-21T04:39:56.303 回答