UITableView 奇怪的行为

发布于 2024-11-14 06:44:40 字数 1796 浏览 2 评论 0原文

我正在使用 UITableView 编写首选项面板,如下所示: 在此处输入图像描述

第一部分由简单的行组成,第二部分由 UISwitch 作为子视图的行组成。

当我更改为横向并开始滚动时,桌面视图的行为方式很奇怪: 在此处输入图像描述

这是我的代码:

- (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;
}

提前谢谢您:)

I'm programming a preferences panel with a UITableView which looks like this:
enter image description here

The first section consists in simple rows and the second section in rows with a UISwitch as a subview.

When I change to landscape and start scrolling, the tableview behaves in a strange way:
enter image description here

This is my code:

- (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;
}

Thank you in advance :)

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

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

发布评论

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

评论(3

吖咩 2024-11-21 06:44:40

您应该执行以下操作:

继续对所有单元格使用相同的 CellIdentifier(性能!如果您为每个单元格创建一个新的标识符,它将起作用,但会消耗大量内存)。仅当您的单元格根本不同时(例如不同的自定义 UITableViewCell 子类),使用多个单元格标识符才合适。

不要将开关添加为子视图 (!!!),而是使用 cell.accessoryView = mySwitch。如果此单元不应有开关,请务必设置 cell.accessoryView = nil 来清理单元以供重用。为您的开关使用accessoryView 将自动处理所有布局工作。

当您将accessoryView设置为nil时,您可以在重用单元格时继续为DetailDisclosure按钮使用accessoryType!

you should do just the following:

Continue to use the same CellIdentifier for all cells (performance! if you create a new identifier for each cell it will work, but consume lots of memory). Using more than one Cell Identifier is only appropriate when your cells are fundamentally different, like different custom UITableViewCell-Subclasses.

don't add your switches as subviews (!!!), use cell.accessoryView = mySwitch instead. if this cell should not have a switch, be sure to set cell.accessoryView = nil to clean up a cell for reuse. Using accessoryView for your Switches will handle all the layout work automatically.

when you set accessoryView to nil, you can continue to use accessoryType for your DetailDisclosure-Buttons when you reuse cells!

千鲤 2024-11-21 06:44:40

发生这种情况的原因可能是您使用相同的单元格标识符来访问表格单元格。

尝试用

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d_%d",indexPath.section,indexPath.row];

代替。

static NSString *CellIdentifier = @"Cell";

It could be happen because you are using the same cell identifier to access the table cell.

Try with

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d_%d",indexPath.section,indexPath.row];

instead of.

static NSString *CellIdentifier = @"Cell";
心房的律动 2024-11-21 06:44:40

tableView 正在回收您添加了 browserSwitch、lastQuerySwitch 和 KeyboardSwitch 的单元格。没有必要将它们从单元格视图中删除。在 // 配置单元格注释之后添加以下行。我想这样就能解决问题了。

  [browserSwitch removeFromSuperview];
  [lastQuerySwitch removeFromSuperview];
  [keyboardSwitch removeFromSuperview];

The tableView is recycling the cells to which you've added your browserSwitch, lastQuerySwitch and keyboardSwitch. There's no point at which these get removed from the cell view. Add the following lines just after // configure the cell comment. I think that'll sort it.

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