【问题标题】:Show alert if a UITableView has no entries如果 UITableView 没有条目,则显示警报
【发布时间】:2012-05-08 20:28:52
【问题描述】:

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

感谢和问候

电脑

【问题讨论】:

  • 请提供一些代码。如何将数据放入 tableview 中?

标签: objective-c ipad uitableview ios5


【解决方案1】:

检查数据源的计数。

例如,如果您使用数组myArrayData

if ([myArrayData count] == 0) {
    // Do code here
}

【讨论】:

    【解决方案2】:

    除了亚当约翰逊的回答

    如果您的数据来自 Core Data 并且您正在实施 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];
    }
    

    【讨论】:

      【解决方案3】:

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

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

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

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

      【讨论】:

        【解决方案4】:

        如果你使用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];
        }
        

        【讨论】:

          【解决方案5】:

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

          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!
          }
          

          【讨论】:

          • 这是一种非常低效的方法。这些单元格是从数据源生成的,应该检查数据源,而不是它创建的视图。
          猜你喜欢
          • 2013-02-19
          • 2018-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-17
          • 2015-08-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多