numberofrow(TableView)为1时如何弹出UIAlertView

How to pop up UIAlertView when the numberofrow (TableView) is 1

我有一个 'add' 按钮来创建新的表格视图行。并且行数限制为1。

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 1;
}

我希望按钮在只有一行时弹出警告视图,不允许创建新行。

但是我不知道如何在按钮操作中实现 'if condition',如下所示:

    - (IBAction)add
{
    if (condition)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Hi" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
}

    [alert show];
    }

请帮忙!抱歉不够专业,我正在努力学习objective c

我认为你需要一个 mutableArray 来保存 UITableView 的数据和 return 数组的计数通过 "numberOfRowsInSection" 方法提供给 UITableView,当你想添加一行时,你只需添加将数据放入数组并重新加载 tableView。

ps:你不能添加行但是 return 1 到 UITableView

您只需-

- (IBAction)add
{
  if ([self.tableView numberOfRowsInSection:0] == 1)
   {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Hi" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
   }

  [alert show];
}

创建布尔变量shouldCreateRow.

将tableview数据源方法写成

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
     if(shouldCreateRow){
      return 1;
    }
}

动作方法为

- (IBAction)add:(UIButton *)sender {

sender.selected=!sender.selected;

if (sender.selected && !shouldCreateRow) {

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"One row created" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

    [alert show];

    shouldCreateRow=TRUE;
    [myTable reloadData];

}else{

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Already there is One row." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

    [alert show];
}
}