【问题标题】:How to select the UITableViewCell from top to bottom in objective c?如何在目标c中从上到下选择UITableViewCell?
【发布时间】:2017-12-13 07:14:18
【问题描述】:

我是Objective C 的新手。我有 UITableView,因为我必须从上到下选择 tableview 单元格,也没有选择第一个单元格,第二个单元格将不会被选择,用户也无法选择中间单元格。谁能帮我解决这个问题?
我在此处添加了示例图像,如果用户选择了第二个单元格要启用的第一个单元格,并且如果用户选择了第二个单元格,则第三个单元格要启用其余单元格要禁用。

- (void)viewDidLoad {
        [super viewDidLoad];

        self.colors = [[NSArray alloc] initWithObjects: @"Red", @"Yellow", @"Green",
                       @"Blue", @"Purpole", nil];
    }


    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return [self.colors count];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"cell";
        UITableViewCell *cell = [tableView
                                 dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        cell.textLabel.text = [self.colors objectAtIndex:indexPath.row];

        return cell;
    }

【问题讨论】:

  • 你需要再写你想做什么?画它什么的
  • 你最好用真实的场景来解释......
  • 感谢您的回复,作为您请求的一部分,我编辑了我的问题
  • @user19 请使用didSelectRowAtIndexPath 方法显示选择或取消选择功能的真实代码更新。

标签: ios objective-c uitableview


【解决方案1】:

您的问题不清楚,希望您在进行多个单元格选择时遇到问题: 允许UITableView 通过tableView.allowsMultipleSelection = true; 选择多个单元格。然后用户可以选择表格中任意数量的单元格。

【讨论】:

  • 感谢您的回答,作为您请求的一部分,我编辑了我的问题。我需要从上到下进行多项选择,而不选择用户不应该选择下一个单元格的第一个单元格。
  • 您需要将您的列标题移动到 UITableView 标题,然后您将不会得到列标题行的复选标记。否则,如果您想使用相同的方法,请使用 UITableView Delegate tableView:( didSelectRowAtIndexPath: 并检查索引 0 并返回: if (indexPath.row == 0) return;
  • 如果第一个单元格被选中,我不需要我需要的标题,第二个单元格想要启用就是这样
  • 然后使用 UITableView Delegate tableView:didSelectRowAtIndexPath: 并获取选定的索引,例如:indexPath.row 并为 indexPath.row+1 调用启用操作
【解决方案2】:

使用这种方式,您可以选择当前行并启用下一行进行选择。您需要处理另一种方法来禁用 cellForRowAtIndexPath 方法中的其他行。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.checkMark.selected = YES;
    NSString *cellText = cell.textLabel.text;

   if(self.colors.count < indexPath.row+1  ){
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
     UITableViewCell *nextCell = [tableView cellForRowAtIndexPath:nextIndexPath];
     nextCell.checkMark.userInteractionEnabled = YES;
   //OR
     nextCell.userInteractionEnabled = YES;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    相关资源
    最近更新 更多