2011-10-02 116 views
0

我正在使用indexpathforvisiblerows,它给出了uitableview中可见行的索引路径。我如何检查返回的索引路径中的空白单元格?如何检查UITableViewCells是否为空

`NSArray * visiblePaths = [self.rootViewController.accountTableView indexPathsForVisibleRows];

为(NSIndexPath * indexPath在visiblePaths) {

UITableViewCell *cell = [self.rootViewController.accountTableView cellForRowAtIndexPath:indexPath]; 
    // I am getting the issue when i call this function 
     cell.detailTextLabel.text = [self getLastCallDate:indexPath]; 


} 

// getLastCallDate功能

帐户*帐户= [self.fetchedResultsController objectAtIndexPath:indexPath]; NSString * lastCallDate;

if ([[account Calls] count] > 0) { 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"YYYY-MM-dd"]; 

    Call *call = [[account Calls] objectAtIndex:0]; 

    lastCallDate = [NSString stringWithFormat:@"Last Call Date: %@",[dateFormatter stringFromDate:call.date]]; 

    //NSLog(@"detail - %@", cell.detailTextLabel.text); 
    [dateFormatter release]; 
    return lastCallDate; 
} 
else 
    lastCallDate [email protected]""; 
return lastCallDate; 

` 在寻找读取的结果控制器不会有多达导致数组边界execption

回答

1

我假设你有一个普通的UITableView不节头中的可见行中的值,就行。然后显示在显示器tableView.frame.size.height/tableView.rowHeight上的行数。减去可见行的数量。

example for empty rows vs. visible cells

通过上面的算法,我得到total rows 16 visible cells 5

现在,您可以计算16 - 5

+0

不,我有几个tablecells有数据,其余都是空白的。我想要有数据的单元格的数量 – Vijay

+0

@Vijay我已经更新了我的答案。 –

2

在致电getLastCallDate:之前,请检查indexPath.row是否小于数据源中的项目数。类似的东西 -

for (NSIndexPath *indexPath in visiblePaths) 
{ 
    if(indexPath.row < [self.items count]) //items is your data source array 
    { 
      //your code 
    } 
}