-1

在我的 iPad 应用程序中,如果 UITableView 上没有条目或所有单元格都是空的,并且如果按下特定按钮,则应该有警报。检查 UITableView 中所有单元格是否为空的条件是什么?

谢谢并恭祝安康

个人电脑

4

5 回答 5

5

检查数据源的计数。

例如,如果您使用的是数组myArrayData

if ([myArrayData count] == 0) {
    // Do code here
}
于 2012-05-08T20:32:41.673 回答
1

除了亚当约翰逊的回答

如果您的数据来自核心数据并且您正在实施NSFetchedResultController

if ([self.fetchedResultsController.fetchedObjects count] > 0) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
    message:@"My message" delegate:self cancelButtonTitle:@"Cancel"
    otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];
}
于 2012-05-08T20:37:50.617 回答
0

您可以使用 viewWillAppear 方法来检查您的要求。

如果您的按钮被按下(我假设在不同的视图控制器上?)您可能希望在按下按钮时实际设置一个 BOOL 并在此视图控制器中引用它。

通常表格视图使用数组(或某些对象)来保存它们的值,您可以简单地检查是否 (myarray.count > 0) 以查看是否有任何表格单元格。

如果您留下有关您的设置的更多信息,我很乐意更具体地满足您的需求。祝你好运!

于 2012-05-08T20:37:20.633 回答
0

如果使用 CoreData,可以通过 numberOfRowInSection 检测写入表的行数如下

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

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];

    if (sectionInfo.numberOfObjects == 0) {

              //Here you can use your code
    }

    return [sectionInfo numberOfObjects];
}
于 2014-03-15T04:31:50.530 回答
-1

好吧,您可以枚举所有单元格并查看它们是否包含任何内容。例如:

BOOL isEmpty = false;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
// Then depending on what kind of value you have in it...
if (cell.textLabel.text == @""){
// Keep going through the enumeration until there's actually something and then set isEmpty to true!
}
于 2012-05-08T20:32:56.580 回答