4

鉴于indexPathUITableViewCell,我想知道该单元格当前是否存在。

我知道如何检查它是否在屏幕上(使用tableView.indexPathsForVisibleRows)。但是,我也想知道它是否不在屏幕上但已经创建(假设用户已经滚动但还没有完全进入屏幕)。

我该怎么做呢?

4

6 回答 6

10

你可以做

[self.tableView cellForRowAtIndexPath:indexPath];

(不要与数据源方法混淆[self tableView:cellForRowAtIndexPath:])如果该索引路径的单元格存在,它将被返回。否则,你会得到nil.

您绝对不希望您的后台更新过程直接引用该单元格,因为正如您所说,它可能已经滚出屏幕并在获取完成时被回收。相反,保留对索引路径或可用于查找索引路径的某些数据的引用,然后通过上述方法使用该索引路径来检索单元格。

于 2013-08-11T05:12:06.337 回答
2

唯一存在的单元格是当前可见的单元格。一旦单元格滚动到视图之外,单元格对象就可供UITableViewCelldequeueReusableCellWithIdentifier方法使用,这就是操作系统在处理要在如此少量的空间中显示的大量表格数据时节省内存的方式。

如果您想跟踪哪些单元格已经被看到,您应该修改底层对象以在对象数据被馈送到应用程序的“ cellForRowAtIndexPath:”方法中的表格视图单元格时设置某种 BOOL 或值。

于 2013-08-11T04:14:34.713 回答
2

如果您通过 重用单元格dequeueReusableCellWithIdentifier:,则 tableView 将像传送带一样执行。已经滚出屏幕的单元格将立即转移到即将进入屏幕的单元格。从理论上讲,tableView 不会为您创建比屏幕更多的单元格。因此,您想了解屏幕外的单元格是没有意义的。

UITableViewCell只是根据MVC Pattern的一个视图。是你的模型,你的数据,决定了在特定 indexPath 的特定单元格中应该出现什么。你的代码可能会是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // dequeue a cell
    // ...
    if (!cell)
    {
        // init a cell
        // ...
    }

    // for a given indexPath, decide what should be presented in the cell, something like updating the cell's properties
    cell.textLabel.text = [self.data dataShouldBePresentedAtIndexPath:indexPath];
}
于 2013-08-11T04:49:52.170 回答
0

或者你可以做这样的事情

-(BOOL) isRowPresentInTableView:(int)row withSection:(int)section
{
    if(section < [self.tableView numberOfSections])
    {
        if(row < [self.tableView numberOfRowsInSection:section])
        {
            return YES;
        }
    }
    return NO;
}
于 2014-10-22T09:32:35.587 回答
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // dequeue a cell
    // ...
    if (cell == nill)
    {
        // init a cell
        // ...
    }

    // for a given indexPath, decide what should be presented in the cell, something like updating the cell's properties
    cell.textLabel.text = [self.data dataShouldBePresentedAtIndexPath:indexPath];
}

您可以检查单元格是否存在....希望这有效... :)

于 2013-08-14T05:41:16.170 回答
0

斯威夫特 3

let YOURINDEXPATH = IndexPath(row: 4, section: 0)

if tableView.cellForRow(at: YOURINDEXPATH) != nil {

    // DO YOUR STUFFS HERE
}

相应地定义您的索引路径(YOURINDEXPATH)

于 2016-12-29T07:01:39.083 回答