iphone - UITableView 奇怪的行为

标签 iphone xcode ios uitableview landscape

我正在编写一个带有 UITableView 的首选项面板,如下所示:
enter image description here

第一部分包含简单的行,第二部分包含 UISwitch 作为 subview 的行。

当我更改为横向并开始滚动时,表格 View 的行为方式很奇怪:
enter image description here

这是我的代码:

- (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/

相关文章:

ios - Sprite Kit 粒子发射器形状

iPhone : Hiding/showing Toolbar

swift - 用户默认值尝试设置非属性列表对象

ios - 找出我的异步调用何时完成

ios - 无法将类型 'UIView' 的值转换为 'SCNView'

ios - 使用大的导航标题和适合的文本

ios - 在 Mavericks OSX 下生成的 iBeacons 可以工作,但与 iOS 有一些不同的字节

android - ffmpeg 给 vcodec 两个参数

iphone - 更改 Three20 导航栏 TintColor 导致图像库缩略图 View 顶部出现间隙

ios - 错误 ITMS-90363 : "Invalid Info.plist key. The key ' CFBundleIcons~ipad' in bundle MyApp. 应用程序……