UITableView,在选择时更改配件时出现问题 [英] UITableView, having problems changing accessory when selected

查看:115
本文介绍了UITableView,在选择时更改配件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UITableView来选择一个(很多)项目。与选择铃声时的UI类似,我想要检查所选项目,而不是其他项目。我想在触摸时选择单元格,然后动画回原始颜色(再次,就像铃声选择UI一样)。

I'm using a UITableView to allow selection of one (of many) items. Similar to the UI when selecting a ringtone, I want the selected item to be checked, and the others not. I would like to have the cell selected when touched, then animated back to the normal color (again, like the ringtone selection UI).

UIViewController子类是我的表的委托和datasource(不是UITableViewController,因为我还有一个工具栏)。

A UIViewController subclass is my table's delegate and datasource (not UITableViewController, because I also have a toolbar in there).

我在cellForRowAtIndexPath:中设置单元格的accessoryType,并在单元格时更新我的​​模型在didSelectRowAtIndexPath中选择:我能想到将所选单元格设置为复选标记(并清除前一个单元格)的唯一方法是在didSelectRowAtIndexPath:中调用[tableView reloadData]。但是,当我这样做时,单元格取消选择的动画很奇怪(单元格的标签应该出现一个白框)。如果我不调用reloadData,当然,accessoryType不会改变,因此不会出现复选标记。

I'm setting the accessoryType of the cells in cellForRowAtIndexPath:, and updating my model when cells are selected in didSelectRowAtIndexPath:. The only way I can think of to set the selected cell to checkmark (and clear the previous one) is to call [tableView reloadData] in didSelectRowAtIndexPath:. However, when I do this, the animating of the cell deselection is weird (a white box appears where the cell's label should be). If I don't call reloadData, of course, the accessoryType won't change, so the checkmarks won't appear.

我想我可以关闭动画,但这似乎很蹩脚。我也玩弄了改变didSelectRowAtIndexPath:中的细胞,但这是一个很大的痛苦。

I suppose I could turn the animation off, but that seems lame. I also toyed with getting and altering the cells in didSelectRowAtIndexPath:, but that's a big pain.

任何想法?缩写代码如下...

Any ideas? Abbreviated code follows...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:kImageCell];
    if( aCell == nil ) {
        aCell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:kImageCell];
    }

    aCell.text = [imageNames objectAtIndex:[indexPath row]];
    if( [indexPath row] == selectedImage ) {
        aCell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        aCell.accessoryType = UITableViewCellAccessoryNone;
    }
    return aCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    selectedImage = [indexPath row]
    [tableView reloadData];
}


推荐答案

我有同样的问题从来没有解决过它。

I had this exact same problem and never solved it.

最后这对我来说没有实际意义,因为我必须听取模型的变化(如果连接,选择可能会改变用户的控制权因此我只是改变了模型并让通知重新加载数据,但它看起来并不太漂亮(它就像一个非动画的取消选择,但是一个循环迭代迟到了!)

In the end it was moot for me because I had to listen for changes in the model (the selection could change outwith the user's control if a connection was lost) so I just changed the model and let the notification reload the data, but it didn't look too pretty (it was like a non-animated deselection, but one run loop iteration late!)

我怀疑唯一的方法是,正如你的建议,保留对单元格本身的引用缓存。如果你编写一个UITableViewCell子类来封装逻辑,那么这应该不会太乱。覆盖-prepareForReuse。

I suspect the only way to do it is, as you suggest, keeping a cache of references to the cells themselves. This shouldn't be too messy if you write a UITableViewCell subclass to encapsulate the logic, overriding -prepareForReuse.

仍然,不漂亮,如果Apple的铃声人员有要做到这一点,他们应该更难以修复UITableView团队!

Still, not pretty, and if the ringtone guys at Apple had to do it this way, they ought to have pressed the UITableView team harder to fix it!

编辑:

嗯,你知道什么?!毕竟这真的很容易。 - [UITableView cellForRowAtIndexPath]将为您提供实际的单元格(如果它们在屏幕外,则为nil),您只需更改accessoryType。

Well, what do you know?! This is really easy after all. -[UITableView cellForRowAtIndexPath] will give you the actual cells (or nil if they're offscreen) and you can just change the accessoryType.

这篇关于UITableView,在选择时更改配件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆