如果所有行都有复选标记,则启用一个按钮
If all Rows have Checkmark, then Enable a button
我有一个 UITableView
,当所有行都有 Checkmark
.
时,我需要 启用导航栏按钮
有办法吗?
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
tableData = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(print_Message)];
self.navigationItem.rightBarButtonItem.enabled = NO;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didSelectRow");
UIAlertView *messageAlert = [[UIAlertView alloc] initWithTitle:@"Row Selected" message:[tableData objectAtIndex:indexPath.row] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[messageAlert show];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
只要 cellForRowAtIndexPath:
不会在索引路径上被多次调用(例如在滚动、强制重新加载、视图重新出现等情况下),您就可以利用您的 UITableView
的 indexPathsForSelectedRows
来帮助确定是否所有单元格都被选中。
为了让 indexPathsForSelectedRows
正常工作,需要对您的代码进行一些更改。
(1) 在你的 viewDidLoad
.
中添加 self.tableView.allowsMultipleSelection = YES;
(2) 在初始代码中,您在方法末尾使用 didSelectRowAtIndexPath:
和 deselectRowAtIndexPath:
。相反,使用 didSelectRowAtIndexPath:
和 didDeselectRowAtIndexPath:
以便选择选中标记的行而不选择未选中标记的行。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
tableData = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
self.tableView.allowsMultipleSelection = YES;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(print_Message)];
self.navigationItem.rightBarButtonItem.enabled = NO;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// If the number of selected index paths = the number of rows,
// enable the button
NSArray *selectedIndexPaths = [tableView indexPathsForSelectedRows];
if (selectedIndexPaths.count == [tableView numberOfRowsInSection:0]) {
self.navigationItem.rightBarButtonItem.enabled = YES;
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
self.navigationItem.rightBarButtonItem.enabled = NO;
}
我有一个 UITableView
,当所有行都有 Checkmark
.
有办法吗?
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
tableData = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(print_Message)];
self.navigationItem.rightBarButtonItem.enabled = NO;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didSelectRow");
UIAlertView *messageAlert = [[UIAlertView alloc] initWithTitle:@"Row Selected" message:[tableData objectAtIndex:indexPath.row] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[messageAlert show];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
只要 cellForRowAtIndexPath:
不会在索引路径上被多次调用(例如在滚动、强制重新加载、视图重新出现等情况下),您就可以利用您的 UITableView
的 indexPathsForSelectedRows
来帮助确定是否所有单元格都被选中。
为了让 indexPathsForSelectedRows
正常工作,需要对您的代码进行一些更改。
(1) 在你的 viewDidLoad
.
self.tableView.allowsMultipleSelection = YES;
(2) 在初始代码中,您在方法末尾使用 didSelectRowAtIndexPath:
和 deselectRowAtIndexPath:
。相反,使用 didSelectRowAtIndexPath:
和 didDeselectRowAtIndexPath:
以便选择选中标记的行而不选择未选中标记的行。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
tableData = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
self.tableView.allowsMultipleSelection = YES;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(print_Message)];
self.navigationItem.rightBarButtonItem.enabled = NO;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// If the number of selected index paths = the number of rows,
// enable the button
NSArray *selectedIndexPaths = [tableView indexPathsForSelectedRows];
if (selectedIndexPaths.count == [tableView numberOfRowsInSection:0]) {
self.navigationItem.rightBarButtonItem.enabled = YES;
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
self.navigationItem.rightBarButtonItem.enabled = NO;
}