【发布时间】:2014-07-17 04:46:46
【问题描述】:
直到现在我还没有解决这个问题,
两个进程,
-
我在 tableview 中有一个按钮,如果我单击该按钮,则显示该部分中的所有复选标记,否则不显示(切换功能)。
-
每一个单元格被点击显示复选标记,否则没有。
我已经完成了第一个案例,但我也想要第二个。
我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = nil;
NSString *key = [_keys objectAtIndex:indexPath.section];
NSArray *name = [_names objectForKey:key];
static NSString *MyCellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyCellIdentifier];
cell.textLabel.text = [name objectAtIndex:indexPath.row];
if([selectAll.currentTitle isEqualToString:@"Deselect All"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
selectAll.frame = CGRectMake(frame.size.width-90, 5, 86, 30);
}
else {
selectAll.frame = CGRectMake(cmdSwitch.frame.origin.x, 0, 90, 30);
}
if([[NSUserDefaults standardUserDefaults] valueForKey:@"Sel_Check"]==nil)
{
[selectAll setTitle:@"Deselect All" forState:UIControlStateNormal];
}
else
{
if([[[NSUserDefaults standardUserDefaults] valueForKey:@"Sel_Check"] isEqualToString:@"1"])
{
[selectAll setTitle:@"Deselect All" forState:UIControlStateNormal];
}
else if([[[NSUserDefaults standardUserDefaults] valueForKey:@"Sel_Check"] isEqualToString:@"0"])
{
[selectAll setTitle:@"Select All" forState:UIControlStateNormal];
}
}
selectAll.layer.cornerRadius = 7;
selectAll.clipsToBounds = YES;
selectAll.titleLabel.font = [UIFont boldSystemFontOfSize:15];
selectAll.backgroundColor = [UIColor whiteColor];
selectAll.layer.borderColor = [UIColor colorWithRed:155.0f/255.0f green:155.0f/255.0f blue:155.0f/255.0f alpha:1.0f].CGColor;
selectAll.layer.borderWidth = 1;
[selectAll setTitleColor:[UIColor colorWithRed:43.0f/255.0f green:65.0f/255.0f blue:116.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[selectAll setExclusiveTouch:YES];
[selectAll addTarget:self action:@selector(mySelect:) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:selectAll];
}
return headerView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self checkedCellAtIndex:indexPath.row];
if([self getCheckedForIndex:indexPath.row]==YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
//我的按钮功能
- (void) mySelect:(NSIndexPath *)indexPath {
if([selectAll.currentTitle isEqualToString:@"Deselect All"])
{
[selectAll setTitle:@"Select All" forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setValue:@"0" forKey:@"Sel_Check"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
[selectAll setTitle:@"Deselect All" forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"Sel_Check"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[self.tableView reloadData];
}
像这样,
【问题讨论】:
-
什么这么复杂?如果选择单元格检查以查看是否选择了所有单元格。 span>
-
您想通过单击单元格来选择几个特定的单元格,或者您只想通过单击 abouve 按钮来选择所有单元格并再次单击它时取消选择??
标签: ios objective-c xcode