[英]how to select, deselect and individual table cell
直到現在我還沒有解決方案,
兩個過程
我在表格視圖中有一個按鈕,如果單擊按鈕,則顯示該部分中的所有復選標記,否則不顯示(切換功能)。
單擊每個單元格都顯示選中標記,否則不顯示。
我已經做了第一種情況,但我也想第二種。
我的代碼:
- (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];
}
像這樣,
您應該將解決方案重新考慮為否。 1。 您應該具有一個NSMutableSet
其中包含所選單元格的NSIndexPath
。
當您僅點擊一個單元格時,將該索引路徑添加到集合中或將其刪除(如果已經存在),然后重新加載選定的行。
當您點擊表格上方的按鈕時,您可以將所有現有索引路徑添加到集合中或清空集合。 然后當然要調用[self.tableView reloadData]
。
在tableView:cellForRowAtIndexPath:
條件將更改為:
if([_indexPathsOfSelectedRows containsObject:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = nil;
static NSString *MyCellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyCellIdentifier];
cell.textLabel.text = [candidates objectAtIndex:indexPath.row];
if([self.selectButton.titleLabel.text isEqualToString:@"Deselect All"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
return cell;
}
Steps to follow:
(1) Make an array (i.e arrCheckedItems) for holding checked cell texts (i.e. @"Five Cents" , @"Ten Cents").
(2) When user select any cell, add that cell's text to arrCheckedItems and when deselect remove that text from array. Then reload the table.
(3) in cellForRowAtIndexPath: method, check cell's text-
if([arrCheckedItems containsObject: cell.text])
{
// cell.text = @"Five Cents" or @"Ten Cents"
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
如果您需要選擇或取消選擇單元格,請選擇:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.