如何在行数(表视图)为 1 时弹出 UIAlertView



我有一个"添加"按钮来创建新的表视图行。并且行数限制为一行。

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

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

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

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

请帮忙!抱歉不专业,我正在尝试学习客观的 c

您所

要做的就是 -

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

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

ps:不能添加行,但返回 1 到 UITableView

创建布尔变量 shouldCreateRow

将表视图数据源方法编写为

 - (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];
}
}

最新更新