为了解决这些谁说“这是一个坏主意”,在我的情况,我需要这是我对我的UITableViewCell
一个按钮,按下时,是SEGUE另一种观点。由于这不是电池本身的选择,所以[self.tableView indexPathForSelectedRow]
不起作用。
这让我两个选择:
- 商店,我需要传递到表格单元本身的视图对象。虽然这会起作用,但它会击败我拥有
NSFetchedResultsController
的点,因为我不需要想要将所有对象存储在内存中,尤其是在表很长的情况下。
- 使用索引路径从提取控制器中检索项目。是的,我不得不通过黑客来找出
NSIndexPath
,但它比存储内存中的对象更便宜。
indexPathForCell:
是用正确的方法,但这里是我会怎么做(假设这段代码在UITableViewCell
子类来实现:
// uses the indexPathForCell to return the indexPath for itself
- (NSIndexPath *)getIndexPath {
return [[self getTableView] indexPathForCell:self];
}
// retrieve the table view from self
- (UITableView *)getTableView {
// get the superview of this class, note the camel-case V to differentiate
// from the class' superview property.
UIView *superView = self.superview;
/*
check to see that *superView != nil* (if it is then we've walked up the
entire chain of views without finding a UITableView object) and whether
the superView is a UITableView.
*/
while (superView && ![superView isKindOfClass:[UITableView class]]) {
superView = superView.superview;
}
// if superView != nil, then it means we found the UITableView that contains
// the cell.
if (superView) {
// cast the object and return
return (UITableView *)superView;
}
// we did not find any UITableView
return nil;
}
PS我真正的代码做这个访问的所有从表中的观点,但我给的,为什么有人会想直接做这样的事情在表格单元格为例
就像从你的其他[问题](http://stackoverflow.com/questions/15711645/how-to-get-uitableview-from-uitableviewcell)获取单元格中的“UITableView” - 为什么?这看起来像是一个糟糕的设计决定的代码味道 - 对于一个单元来说,它没有太多合法的用例来知道它是否是indexPath,或者它是否在屏幕上。 –
我同意Paul.s的评论。你应该说明你为什么要这样做,因为你想要做的事可能是一个坏主意。 – rdelmar
@ Paul.s我在问题中发布了我的程序设计。这可能是不好的编程实践,在这种情况下,如果你可以提出一个替代方案,那将是非常有用的。一般来说,我对可可非常陌生,对编程也比较陌生。 –