【问题标题】:How to select single row in every section?如何在每个部分中选择单行?
【发布时间】:2015-01-06 06:37:19
【问题描述】:

我正在使用情节提要构建一个 iOS 应用。我已经用两个部分实现了UITableView,例如

Alpha
 a
 b  >
Numb
 1  >
 2
 3 

我想在每个部分中选择单行并获取所选行的值。

我做不到,谁能帮帮我。

这是我的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Uncheck the previous checked row
    long sec = [indexPath section];
    if(sec==0){
    if(self->checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                                        cellForRowAtIndexPath: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;
    }
    }
    else if(sec==1 ){
        if(self->checkedIndexPath)
        {
            UITableViewCell* uncheckCell = [tableView
                                            cellForRowAtIndexPath:self->checkedIndexPath];
            uncheckCell.accessoryType = UITableViewCellAccessoryNone;
        }
        if([self->checkedIndexPath isEqual:indexPath])
        {
            self->checkedIndexPath = nil;
        }
    }
}

【问题讨论】:

  • 为您的 UITableView 实例设置多项选择 = ON。

标签: ios objective-c uitableview didselectrowatindexpath


【解决方案1】:

取消选择同一部分中的任何选定行。这样一次只能选择一行。

- (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    for ( NSIndexPath* selectedIndexPath in tableView.indexPathsForSelectedRows ) {
        if ( selectedIndexPath.section == indexPath.section )
            [tableView deselectRowAtIndexPath:selectedIndexPath animated:NO] ;
    }
    return indexPath ;
}

【讨论】:

  • 嗨@user 感谢您的快速回复,这在我的情况下不起作用。我只能在 sec o 中选择一行
猜你喜欢
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 2017-02-26
  • 2014-10-14
相关资源
最近更新 更多