在UITableView ios的每个部分中选择一行?

在UITableView ios的每个部分中选择一行?,ios,objective-c,uitableview,didselectrowatindexpath,Ios,Objective C,Uitableview,Didselectrowatindexpath,情景: 我在一个UITableView中创建了两个部分,用户需要在每个部分中选择一行,如下面的屏幕截图所示 预期成果: 1.用户应该能够在每个部分中选择一行 目前的结果: 1.在我选择了一个部分中的行,然后在第二个部分中选择该行时,第一个选择将消失 这是我的密码: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Uncheck the prev

情景:

我在一个UITableView中创建了两个部分,用户需要在每个部分中选择一行,如下面的屏幕截图所示

预期成果: 1.用户应该能够在每个部分中选择一行

目前的结果: 1.在我选择了一个部分中的行,然后在第二个部分中选择该行时,第一个选择将消失

这是我的密码:

- (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;
    }}

    if(sec==1){

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

    }

}

谢谢你的帮助

您可以在tableview中启用多重选择:

self.tableView.allowsMultipleSelection = YES;
这是最简单的方法。 最后我找到了解决办法。它对我有用,希望它对你有用。 申报这些

@interface ViewController ()
{
    int selectedsection;
    NSMutableArray *selectedindex;

}
按如下方式替换didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Uncheck the previous checked row

    NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

        if(self.checkedIndexPath)

        {

            for (int i=0; i<[selectedindex count]; i++) {

                NSIndexPath *temp= [selectedindex objectAtIndex:i];
                if (temp.section==selectedIndexPath.section) {
                    UITableViewCell* uncheckCell = [tableView
                                                    cellForRowAtIndexPath:temp];
                    uncheckCell.accessoryType = UITableViewCellAccessoryNone;

                }
            }

            NSInteger numb= [tableView numberOfRowsInSection:selectedIndexPath.section];

            if (selectedsection==selectedIndexPath.section) {


            UITableViewCell* uncheckCell = [tableView
                                            cellForRowAtIndexPath:self.checkedIndexPath];
            uncheckCell.accessoryType = UITableViewCellAccessoryNone;

            }
        }



        if([self.checkedIndexPath isEqual:indexPath])
        {
            for (int i=0; i<[selectedindex count]; i++) {

                NSIndexPath *temp= [selectedindex objectAtIndex:i];
                if (temp.section==selectedIndexPath.section) {
                    UITableViewCell* uncheckCell = [tableView
                                                    cellForRowAtIndexPath:temp];
                    uncheckCell.accessoryType = UITableViewCellAccessoryNone;

                }
            }


           self.checkedIndexPath = nil;
        }
        else
        {
            for (int i=0; i<[selectedindex count]; i++) {

                NSIndexPath *temp= [selectedindex objectAtIndex:i];
                if (temp.section==selectedIndexPath.section) {
                    UITableViewCell* uncheckCell = [tableView
                                                    cellForRowAtIndexPath:temp];
                    uncheckCell.accessoryType = UITableViewCellAccessoryNone;

                }
            }

            UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            self.checkedIndexPath = indexPath;
            [selectedindex addObject:indexPath];
            selectedsection=indexPath.section;

            NSLog(@"check");
        }






}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath{
//取消选中上一个选中的行
NSIndexPath*selectedIndexPath=[tableView indexPathForSelectedRow];
if(self.checkedIndexPath)
{

对于(inti=0;i我编写了一个示例代码,其中一个复合数据源为每个部分保存数据源对象。听起来很复杂,但实际上提供了一个易于扩展的体系结构。并使视图控制器保持小型

这种方法的优点是:

  • 小型视图控制器
  • ViewController设置视图并处理用户交互,这在MVC中应该是这样的
  • 可重用数据源
  • 通过每个部分使用不同的数据源,可以轻松地为每个部分自定义单元格
基本数据源体系结构 这提供了简单的扩展,并且易于重用


数据源体系结构的选择 我们从上面扩展类,以允许每个部分有一个选择

#import "ComoundTableViewDataSource.h"

@interface OnSelectionPerSectionComoundTableViewDataSource : ComoundTableViewDataSource
-(void)selectedCellAtIndexPath:(NSIndexPath *)indexPath;

@end

视图控制器实现 正如承诺的那样,非常纤薄的视图控制器:

@interface ViewController () <UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) OnSelectionPerSectionComoundTableViewDataSource *tableViewDataSource;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableViewDataSource = [[OnSelectionPerSectionComoundTableViewDataSource alloc] initWithTableView:self.tableView];
    self.tableView.delegate = self;
    [self.tableViewDataSource setDataSource:[[SingleSelectionSingleSectionDataSource alloc] initWithArray:@[@"Hallo", @"Welt"]] forSection:0];
    [self.tableViewDataSource setDataSource:[[SingleSelectionSingleSectionDataSource alloc] initWithArray:@[@"Hello", @"World", @"!"]] forSection:1];
    [self.tableViewDataSource setDataSource:[[SingleSelectionSingleSectionDataSource alloc] initWithArray:@[@"Hola", @"Mundo", @"!", @"¿Que tal?"]] forSection:2];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableViewDataSource selectedCellAtIndexPath:indexPath];
}
@end
@界面视图控制器()
@属性(弱、非原子)IBUITableView*tableView;
@属性(非原子,强)OnSelectionPerSectionComoundTableViewDataSource*tableViewDataSource;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
self.tableViewDataSource=[[OnSelectionPerSectionComoundTableViewDataSource alloc]initWithTableView:self.tableView];
self.tableView.delegate=self;
[self.tableViewDataSource setDataSource:[[SingleSelectionSingleSectionDataSource alloc]initWithArray:@[@“Hallo”,@“Welt”]]用于分区:0];
[self.tableViewDataSource setDataSource:[[SingleSelectionSingleSectionDataSource alloc]initWithArray:@[@“Hello”、@“World”、@“!”]]用于分区:1];
[self.tableViewDataSource setDataSource:[SingleSelectionSingleSectionDataSource alloc]initWithArray:@[@“Hola”、@“Mundo”、@“!”、@“Que tal?”]用于节:2];
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
[self.tableViewDataSource selectedCellAtIndexPath:indexPath];
}
@结束
您需要向数据源添加方法以获取所选行

获取示例:


注意此代码有一个单元格重用问题。它在GitHub上已修复。

感谢您的回复,我希望在每个部分中只选中一行。然后您应该跟踪每个部分中的选择,如果行中已经选择了某个内容,请取消选择。非常感谢@Sabrina,这对我来说正是我想要的。滚动后tableview所选单元格被禁用,请发布“cellForRowAtIndexPath”代码。
@import UIKit;

@interface SingleSectionDataSource : NSObject <UITableViewDataSource>
@property (nonatomic, strong, readonly) NSArray *array;
@property (nonatomic, strong, readonly) UITableView *tableView;

- (instancetype)initWithArray:(NSArray *)array;
@end
#import "SingleSectionDataSource.h"

@interface SingleSectionDataSource ()
@property (nonatomic, strong, readwrite) NSArray *array;
@property (nonatomic, strong, readwrite) UITableView *tableView;

@end

@implementation SingleSectionDataSource

- (instancetype)initWithArray:(NSArray *)array
{
    self = [super init];
    if (self) {
        self.array = array;
    }
    return self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    self.tableView = tableView;
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.array[indexPath.row];
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [@(section) stringValue];
}

@end
#import "ComoundTableViewDataSource.h"

@interface OnSelectionPerSectionComoundTableViewDataSource : ComoundTableViewDataSource
-(void)selectedCellAtIndexPath:(NSIndexPath *)indexPath;

@end
#import "OnSelectionPerSectionComoundTableViewDataSource.h"
#import "SingleSelectionSingleSectionDataSource.h"

@implementation OnSelectionPerSectionComoundTableViewDataSource


-(instancetype)initWithTableView:(UITableView *)tableView
{
    self = [super initWithTableView:tableView];
    if(self){
        [tableView setAllowsMultipleSelection:YES];
    }
    return self;
}

-(void)selectedCellAtIndexPath:(NSIndexPath *)indexPath
{
    SingleSelectionSingleSectionDataSource *sectionDataSource = self.internalDictionary[@(indexPath.section)];
    [sectionDataSource selectedCellAtIndexPath:indexPath];
}
@end
@interface ViewController () <UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) OnSelectionPerSectionComoundTableViewDataSource *tableViewDataSource;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableViewDataSource = [[OnSelectionPerSectionComoundTableViewDataSource alloc] initWithTableView:self.tableView];
    self.tableView.delegate = self;
    [self.tableViewDataSource setDataSource:[[SingleSelectionSingleSectionDataSource alloc] initWithArray:@[@"Hallo", @"Welt"]] forSection:0];
    [self.tableViewDataSource setDataSource:[[SingleSelectionSingleSectionDataSource alloc] initWithArray:@[@"Hello", @"World", @"!"]] forSection:1];
    [self.tableViewDataSource setDataSource:[[SingleSelectionSingleSectionDataSource alloc] initWithArray:@[@"Hola", @"Mundo", @"!", @"¿Que tal?"]] forSection:2];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableViewDataSource selectedCellAtIndexPath:indexPath];
}
@end