didSelectRowAtIndexPath错误的NSIndexPath

时间:2013-07-24 16:15:28

标签: tableview nsindexpath

我有一个UITableView,我已经分配了部分。当使用带有indexPath.actually的didSelectRowAtIndex时,NSIndexPath参数不正确。 例如 在第1部分第0行中,则indexPath为 [0,1] ; 第2部分第0行<* em>,它得到* [0,2] 。而第0行第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];

}

1 个答案:

答案 0 :(得分:0)

幸运的是我找到了答案,其他人在子类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]];
    }
}

当我删除它时,效果很好

相关问题