Iphone - cellForRowAtIndexPath 向下和向上滚动时行为异常

发布于 2024-11-02 19:52:43 字数 1510 浏览 1 评论 0原文

我想在附件视图上获得一个具有不同元素的 TableView,具体取决于部分。一切正常,但当我向下滚动到第 2 部分,然后再次向上滚动到第 0 部分时,第 0 部分中的行有切换器。你能帮我找出我做错了什么吗? <代码>

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dictionary;
NSArray *array;
NSString *cellValue;
UISwitch *switchView;

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

switch (sectionIndex) {
    case 0:
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        break;
    case 1:
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        break;
    case 2:
        switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
        cell.accessoryView = switchView;
        [switchView setOn:NO animated:NO];
        [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
        [switchView release];
        break;
    default:
        break;
}

dictionary = [searchArray objectAtIndex:indexPath.section];
array = [dictionary objectForKey:@"advancedSearch"];
cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;

}

<代码>

I would like to get a TableView with different elements on the accessoryView, depending on the section. Everything works fine, but when I scroll down to section 2 and then scroll up again to section 0, the rows in section 0 have switchers. Could you please help me finding out what I'm doing wrong?

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dictionary;
NSArray *array;
NSString *cellValue;
UISwitch *switchView;

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

switch (sectionIndex) {
    case 0:
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        break;
    case 1:
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        break;
    case 2:
        switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
        cell.accessoryView = switchView;
        [switchView setOn:NO animated:NO];
        [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
        [switchView release];
        break;
    default:
        break;
}

dictionary = [searchArray objectAtIndex:indexPath.section];
array = [dictionary objectForKey:@"advancedSearch"];
cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迟月 2024-11-09 19:52:43

您正在使表格单元格出队(参见 -[UITableView dequeueReusableCellWithIdentifier:]),这意味着您正在接收已分配并可能预先填充了过时数据的表格视图。在 switch 语句的 case 0:case 1: 中,您应该添加以下行:

cell.accessoryView = nil;

You are dequeuing your table cells (cf. -[UITableView dequeueReusableCellWithIdentifier:]), which means that you are receiving table views that have already been allocated and possibly pre-populated with stale data. In case 0: and case 1: of your switch statement, you should add the following line:

cell.accessoryView = nil;
来世叙缘 2024-11-09 19:52:43

不同的小区使用不同的小区标识符。
类似于:

NSString *CellIdentifier;
switch ([indexPath row]) {
   case 0: CellIdentifier = @"cell0";
           break;
   case 1: CellIdentifier = @"cell1";
           break;
   default: CellIdentifier = @"cell";
           break;
}

否则 Xcode 会认为您的所有单元格具有相同的外观,并在您滚动时尝试重用它们。

Use different cell identifiers for different cells.
Something like:

NSString *CellIdentifier;
switch ([indexPath row]) {
   case 0: CellIdentifier = @"cell0";
           break;
   case 1: CellIdentifier = @"cell1";
           break;
   default: CellIdentifier = @"cell";
           break;
}

Otherwise Xcode thinks that all your cells have the same look and tries to reuse them while you scroll.

胡大本事 2024-11-09 19:52:43

问题来自 dequeueReusableCellWithIdentifier,您必须为不同的单元使用不同的标识符,因为您重用了它们。

The problem is from dequeueReusableCellWithIdentifier you must use different identifiers for different cells because you reuse them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文