关于iOS中cellForRowAtIndexPath的para indexPath的困惑

时间:2014-08-15 17:21:53

标签: ios uitableview

我是iOS开发人员的新手,所以这对你来说可能是一个简单的问题,但我检查苹果开发指南是找不到它。

我有很多UITableViews使用一个dataSourceDelegate,这是我创建它们的方式:

- (void)createSomeTableview:(NSInteger)numberOfTableViews{

    tableViewsArray = [[NSMutableArray alloc] initWithCapacity:numberOfTableViews];

    for (int i = 0; i < numberOfTableViews; i++) {
        UITableView *itemTableView = [[UITableView alloc] initWithFrame:CGRectMake(90, 0, 235, self.view.bounds.size.height) style:UITableViewStylePlain];

    [itemTableView setDataSource:self];
    [itemTableView setDelegate:self];

    [itemTableView registerClass:[KKYItemListCell class] forCellReuseIdentifier:[NSString stringWithFormat:@"%ld", (long)currentSection]];
    [tableViewsArray addObject:itemTableView];

    [[self view] addSubview:itemTableView];
    }
}

当我从服务器上获取数据后,我调用createSomeTableview,然后我使用一个循环来重新加载每个tableView的数据(我在成功响应后的AFNetwoking POST块中调用它):

    for (int i = 0; i < sectionNum;i++){
        currentSection = i;
        [[tableViewsArray objectAtIndex:i] reloadData];
    }

这就是灾难发生的地方!(T T)重新加载过程总是有一个空单元格返回,所以我跟踪重新加载过程并发现了一件奇怪的事情:

调用方法reloadData后,调用方法numberOfRowsInSection(每个表只有一个部分),然后返回调用下一个reloadData tableViewsArray中的Tableview。(我以前认为reloadData会调用cellForRowAtIndexPath

tableViewsArray调用numberOfRowsInSection后的所有观看次数之后,方法cellForRowAtIndexPath已被tableViewsArray中的最后一个视图调用。好吧,它开始构建我的tableview的数据〜,我这样做是为了在数组中指定我的tableViews:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    .....
    if (tableView == sectionTableView) {
        ...
    } else if (tableView == [tableViewsArray objectAtIndex:(long)currentSection]) {
        ...//data handle
        if (itemsNum == 0) { //itemsNum is the count of current table's items
            currentSection--; //if loaded end then go to the next tableView
            return cell;
        }
        itemsNum--;
        return cell;
    }
    return cell;
}

但它有问题!!!我从服务器获取当前数据中的18个项目(itemsNum = 18),我从numberOfRowsInSection得到18个返回。但是当indexPath.row == 6时,tableView的索引是currentSection - 1,这意味着它不再是当前的tableView(实际上它是下一个tableView)正在调用方法`cellForRowAtIndexPath'。

所以(tableView == [tableViewsArray objectAtIndex:(long)currentSection])为false,方法返回nil单元格,然后崩溃。

我想知道方法'tableView:cellForRowAtIndexPath'在indexPath中获取para行的位置是什么?不是'numberOfRowsInSection'的返回?(在我的程序中看起来不像)。

0 个答案:

没有答案