我正在编写一个带有 UITableView 的首选项面板,如下所示:
第一部分包含简单的行,第二部分包含 UISwitch 作为 subview 的行。
当我更改为横向并开始滚动时,表格 View 的行为方式很奇怪:
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell
if (indexPath.section == 0) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (indexPath.row == 0) {
cell.textLabel.text = @"Help";
}else if (indexPath.row == 1){
cell.textLabel.text = @"About us";
}
else if(indexPath.row == 2){
cell.textLabel.text = @"Send to a friend";
}
}else if(indexPath.section == 1){
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == 0) {
cell.textLabel.adjustsFontSizeToFitWidth = NO;
cell.textLabel.text = @"Show favs at launch";
[cell addSubview:favoritesSwitch];
}else if (indexPath.row == 1){
cell.textLabel.text = @"Open with Safari";
[cell addSubview:browserSwitch];
}
else if(indexPath.row == 2){
cell.textLabel.text = @"Remember query";
[cell addSubview:lastQuerySwitch];
}
else if(indexPath.row == 3){
cell.textLabel.text = @"Automatic keyboard";
[cell addSubview:keyboardSwitch];
}
}
return cell;
}
先感谢您 :)
最佳答案
您应该执行以下操作:
继续 对所有单元格使用相同的 CellIdentifier (性能!如果您为每个单元格创建一个新标识符,它将起作用,但会消耗大量内存)。仅当您的单元格根本不同时才适合使用多个单元格标识符,例如不同的自定义 UITableViewCell-Subclass。
不要将您的开关添加为 subview (!!!) , 使用 cell.accessoryView = mySwitch
反而。如果此单元格不应该有开关,请务必设置 cell.accessoryView = nil
清理一个单元格以供重复使用。为您的 Switch 使用 accessoryView 将自动处理所有布局工作。
当您将附件 View 设置为 nil 时,您可以在重用单元格时继续为您的 DetailDisclosure-Buttons 使用附件类型!
关于iphone - UITableView 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6238398/