gpt4 book ai didi

iphone - 如何在 UITableView 中的每个部分打一个复选标记?

转载 作者:行者123 更新时间:2023-12-01 17:09:54 26 4
gpt4 key购买 nike

在我的表格 View 中,我有 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];
    }

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

    最佳答案

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

    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: 上的行的东西

    关于iphone - 如何在 UITableView 中的每个部分打一个复选标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13296116/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com