gpt4 book ai didi

ios - UITableViewController tableView :cellForRowAtIndexPath returns wrong row

转载 作者:行者123 更新时间:2023-12-01 17:52:49 24 4
gpt4 key购买 nike

在我的 Controller 类中,我正在实现 TableView 数据源委托(delegate)方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@"In cell for row at index path %i ", indexPath.row);
// a bunch of other typical stuff
}

在按钮按下时,我想修改某个单元格。为了获得引用,我这样做:
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

而且我确实得到了对单元格的引用,但我的日志方法没有执行。好像 UITableViewController.tableView 有不同的委托(delegate)?当我这样做时:
UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

结果它返回不同的行(它看起来像标题?)。这里有一些 NSLogs 来显示正在发生的事情
       NSLog(@"About to send message to tableView:cellForRowAtIndexPath:indexPathForRow:inSection");
MCSwipeTableViewCell *cell = (MCSwipeTableViewCell *) [self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
MCSwipeTableViewCell *theOtherCell = (MCSwipeTableViewCell *) [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

NSLog(@"Here is the call on self %@", cell);
NSLog(@"Here is the delegate cell %@", theOtherCell);
NSLog(@"Here is my reference %@", self);
NSLog(@"Here is the table view delegate %@", self.tableView.delegate);

和输出:
2014-06-13 15:20:41.601 krow[28823:70b] Here is the call on self <MCSwipeTableViewCell: 0xa3eda00; baseClass = UITableViewCell; frame = (0 0; 320 44); gestureRecognizers = <NSArray: 0x8d82dc0>; layer = <CALayer: 0x8d82de0>>
2014-06-13 15:20:41.601 krow[28823:70b] Here is the delegate cell <MCSwipeTableViewCell: 0xc43fa00; baseClass = UITableViewCell; frame = (0 0; 320 220); autoresize = W; gestureRecognizers = <NSArray: 0xa8be640>; animations = { position=<CABasicAnimation: 0x8d79040>; }; layer = <CALayer: 0xa87d070>>
2014-06-13 15:20:41.601 krow[28823:70b] Here is my reference <KrowInboxTableViewController: 0x8c779c0>
2014-06-13 15:20:41.601 krow[28823:70b] Here is the table view delegate <KrowInboxTableViewController: 0x8c779c0>

为什么我得到 cellForRowAtIndexPath 的不同行?

最佳答案

对于初学者,cellForRowAtIndexPath:您有意调用的方法不会调用委托(delegate)来创建新单元格。与委托(delegate)方法不同 tableView:cellForRowAtIndexPath:它实际上创建或出列一个单元格并将其返回到 TableView ,cellForRowAtIndexPath:方法只是在那里为您提供对现有单元格的引用。文档在返回值的描述中支持了这一点:
An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range. .

再说一遍,tableview 自己调用的委托(delegate)方法是用来创建一个单元格的,而您正在调用的方法只是在那里抓取一个现有的可见单元格,而不需要创建或出列任何东西。

至于下一点,问题在于您正在调用 tableview 应该调用的方法,该方法将创建或出列一个新单元格,而不是返回一个已经可见的单元格。您永远不应手动调用此方法。您使用的第一种方法cellForRowAtIndexPath:是获取现有可见单元格的正确方法。除此之外,如果这不能解决您的问题,您需要提供有关您实际尝试完成的更多信息。

关于ios - UITableViewController tableView :cellForRowAtIndexPath returns wrong row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24214581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com