简体   繁体   中英

How do I put one check mark per section in UITableView?

In my tableview i have 4 sections and i need to have 1 check mark in each section. Like so...

  • Section 0
    • row 0
    • row 1
    • row 2 /
    • row 3
  • Section 1
    • row 0
    • row 1 /
    • row 2

etc..

numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger numberOfRows;
if (section == 0) {
    numberOfRows = 3;
}
else if (section == 1) {
    numberOfRows = 4;
}
else if (section == 2) {
    numberOfRows = 4;
}
else if (section == 3) {
    numberOfRows = 3;
}
return numberOfRows;
}

cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *userChoices = @"UserChoices";
cell = [tableView dequeueReusableCellWithIdentifier:userChoices];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:userChoices];

if (indexPath.section == 0) {
    cell.textLabel.text = [mail objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1) {
    cell.textLabel.text = [search objectAtIndex:indexPath.row];
}
else if (indexPath.section == 2) {
    cell.textLabel.text = [social objectAtIndex:indexPath.row];
}
else if (indexPath.section == 3) {
    cell.textLabel.text = [maps objectAtIndex:indexPath.row];
}

if([self.checkedIndexPath isEqual:indexPath])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

return cell;

}

didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Uncheck the previous checked row
UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
if(self.checkedIndexPath)
{
    uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}

if([self.checkedIndexPath isEqual:indexPath])
{
    self.checkedIndexPath = nil;
}
else
{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.checkedIndexPath = indexPath;
}

// use this to put checkmark on the item
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

The following code gives me a check mark next to a cell within ALL sections of the table. Any body have any ideas?

Make a NSMutableArray of NSNumbers that contain integer representations of the selected element within the sections that is represented by the element in the array.

You will have to do some more set up but when you check

if([self.checkedIndexPath isEqual:indexPath])

you can check

if([[yourArrayOfCheckedIndexes objectAtIndex:indexPath.section] integerValue] == indexPath.row)

Basically you need to store the checked index for every section

You could use a dictionary to keep track of which row is checked on each section:

NSMutableDictionary *_checkedRowsDict = [NSMutableDictionary dictionaryWithCapacity:numberOfSections];

...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_checkedRowsDict objectForKey:[NSNumber numberWithInt:indexPath.section]]) {
        // Unmark the row found on _checkedRowsDict
        NSNumber *num = [_checkedRowsDict objectForKey:[NSNumber numberWithInt:indexPath.section]];
        int row = [num intValue];
        NSIndexPath *checkIndexPath = [NSIndexPath indexPathForRow:row inSection:indexPath.section];

        UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:checkIndexPath];
        checkedCell.accessoryType = UITableViewCellAccessoryNone;
    }

    // Mark the selected row
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;

    // Update _checkedRowsDict
    [_checkedRowsDict setObject:[NSNumber numberWithInt:indexPath.row] forKey:[NSNumber numberWithInt:indexPath.section]];

    ...

}

And something similar to initialize the rows on tableView:cellForRowAtIndexPath:

Try this, worked just fine for me:

-VARIABLES:

@property NSInteger checkedCellRow;
@property NSInteger checkedCellSection;

-FUNCTIONS:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (checkedCellSection == indexPath.section)
{
if (checkedCellRow == indexPath.row)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
checkedCellRow = indexPath.row;
checkedCellSection = indexPath.section;
[myTable reloadData];
}

I add this to my viewDidLoad and all works fine. Thx.

-(void)viewDidLoad {
NSUInteger i = 2; //Number of section
NSMutableDictionary *_checkedRowsDict = [NSMutableDictionary dictionaryWithCapacity:i];
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM