【问题标题】:didSelectRowAtIndexPath with wrong NSIndexPathdidSelectRowAtIndexPath 错误的 NSIndexPath
【发布时间】:2013-07-24 16:15:28
【问题描述】:

我有一个 UITableView 我已经分配了部分。将 didSelectRowAtIndex 与 indexPath.actually 一起使用时,NSIndexPath 参数不正确。 例如 在 section 1 row 0 中,则 indexPath 为 [0,1]section 2 row 0 *, 它得到 *[0, 2]。而 section 0 row 1[0, 1] .似乎第 0 节是正确的。

我很沮丧,不知道为什么? 谁能告诉我如何解决这个问题或给我一些建议?

以下是代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)sender 
{
return _store.count;
}

- (NSInteger)tableView:(UITableView *)sender numberOfRowsInSection:(NSInteger)section {

        if ([_store checkIfHasAdditionalContentInSection:section]) {
             return ([_store additionalContentInSection:section].count + 1);
        }
        return 1;
 }

- (UITableViewCell *)tableView:(UITableView *)sender cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CallLogCell";
    CallLogCell *cell = [sender dequeueReusableCellWithIdentifier:CellIdentifier];
    // Configure the cell...
    if (cell == nil) {
        cell = [[[CallLogCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier] autorelease];
    }
    CallLog *cLog = [_store itemAtIndex:indexPath];
    cell.callTimes = [_store callTimesAtIndex:indexPath];
    cell.callLog = cLog;
    cell.index = indexPath.section;
    [cell update];

    return cell;
}




- (CGFloat)tableView:(UITableView*)sender heightForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell *cell=[self tableView:sender cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)sender didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DLog(@"indexPath %@", [indexPath description]);
**//I always get incorrect answer at here**

DLog(@"indexPath section %d row %d", indexPath.section, indexPath.row); 

[sender deselectRowAtIndexPath:indexPath animated:NO];

}

【问题讨论】:

    标签: tableview nsindexpath


    【解决方案1】:

    幸运的是我找到了答案,其他人在子类 UITableViewCell 中使用奇怪的代码 这是代码:

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [self touchesCancelled:touches withEvent:event];
        UITableView * tableView = (UITableView*)self.superview;
        if ([tableView isKindOfClass:[UITableView class]]) {
            [tableView.delegate tableView:tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
        }
    }
    

    当我删除它时,它运行良好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多