Iphone - cellForRowAtIndexPath 向下和向上滚动时行为异常
我想在附件视图上获得一个具有不同元素的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使表格单元格出队(参见
-[UITableView dequeueReusableCellWithIdentifier:]
),这意味着您正在接收已分配并可能预先填充了过时数据的表格视图。在 switch 语句的case 0:
和case 1:
中,您应该添加以下行: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. Incase 0:
andcase 1:
of your switch statement, you should add the following line:不同的小区使用不同的小区标识符。
类似于:
否则 Xcode 会认为您的所有单元格具有相同的外观,并在您滚动时尝试重用它们。
Use different cell identifiers for different cells.
Something like:
Otherwise Xcode thinks that all your cells have the same look and tries to reuse them while you scroll.
问题来自 dequeueReusableCellWithIdentifier,您必须为不同的单元使用不同的标识符,因为您重用了它们。
The problem is from dequeueReusableCellWithIdentifier you must use different identifiers for different cells because you reuse them.