UITableView 奇怪的行为
我正在使用 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:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该执行以下操作:
继续对所有单元格使用相同的 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 setcell.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!
发生这种情况的原因可能是您使用相同的单元格标识符来访问表格单元格。
尝试用
代替。
It could be happen because you are using the same cell identifier to access the table cell.
Try with
instead of.
tableView 正在回收您添加了 browserSwitch、lastQuerySwitch 和 KeyboardSwitch 的单元格。没有必要将它们从单元格视图中删除。在 // 配置单元格注释之后添加以下行。我想这样就能解决问题了。
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.