2

在我的表格视图中,我有 4 个部分,我需要在每个部分中有 1 个复选标记。像这样...

  • 第 0 节
    • 第 0 行
    • 第 1 行
    • 第 2 行 /
    • 第 3 行
  • 第 1 节
    • 第 0 行
    • 第 1 行 /
    • 第 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];
}

以下代码在表格的所有部分中的单元格旁边给我一个复选标记。任何机构有任何想法?

4

4 回答 4

0

您可以使用字典来跟踪每个部分检查了哪一行:

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]];

    ...

}

以及类似于初始化行的东西tableView:cellForRowAtIndexPath:

于 2012-11-08T21:09:34.080 回答
0

试试这个,对我来说效果很好:

-变量:

@property NSInteger checkedCellRow;
@property NSInteger checkedCellSection;

-功能:

- (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];
}
于 2015-01-16T14:48:45.787 回答
0

创建一个由 NSNumbers 组成的 NSMutableArray,其中包含数组中元素表示的部分中所选元素的整数表示。

您将不得不进行更多设置,但是当您检查时

if([self.checkedIndexPath isEqual:indexPath])

你可以检查

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

基本上你需要为每个部分存储检查的索引

于 2012-11-08T19:18:36.783 回答
-1

我将此添加到我的 viewDidLoad 中,一切正常。谢谢。

-(void)viewDidLoad {
NSUInteger i = 2; //Number of section
NSMutableDictionary *_checkedRowsDict = [NSMutableDictionary dictionaryWithCapacity:i];
...
}
于 2013-03-14T12:35:45.980 回答