Objective c 当numberofrow(TableView)为1时如何弹出UIAlertView
我有一个“添加”按钮来创建tableview的新行。并且行数限制为一行Objective c 当numberofrow(TableView)为1时如何弹出UIAlertView,objective-c,uialertview,Objective C,Uialertview,我有一个“添加”按钮来创建tableview的新行。并且行数限制为一行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return 1; } 我希望该按钮在有一行时弹出一个警报视图,不允许创建新行 但是,我不知道如何在按钮操作中实现“如果条件”,如
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
我希望该按钮在有一行时弹出一个警报视图,不允许创建新行
但是,我不知道如何在按钮操作中实现“如果条件”,如下所示:
- (IBAction)add
{
if (condition)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Hi" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
}
[alert show];
}
请帮忙!抱歉,我不太专业,我正在学习objective c我认为您需要一个可变数组来保存UITableView的数据,并通过“numberOfRowsInSection”方法返回提供给UITableView的数组计数,当您要添加行时,您只需将数据添加到数组中并重新加载tableView即可
ps:您不能添加行,但返回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];
}
}
作用方法如下:
- (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];
}
}
如果返回的是行数,则在检查是否应显示警报时使用相同的计算。但是,如果您总是返回一个用于行计数,则警报无法显示。谢谢您告诉我,这非常有意义,非常有用