Xcode tableview 如何在每个部分中选择一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14553323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:38:49  来源:igfitidea点击:

Xcode tableview how to select ONE row in EACH section

xcodetableview

提问by mablecable

I have a tableview with 2 sections. I want only one row to be selected per each section. Right now I have it working to select one row, but it is for the entire table. Ultimately, I want 2 rows selected, but only ONE per SECTION. I have been stuck on this for days and can not figure it out. Is this possible?? I am new to Xcode so any help would be very much appreciated. Thanks in advance!

我有一个包含 2 个部分的 tableview。我希望每个部分只选择一行。现在我可以选择一行,但它是针对整个表的。最终,我希望选择 2 行,但每节仅选择 1 行。我已经被困在这个问题上好几天了,无法弄清楚。这可能吗??我是 Xcode 的新手,因此非常感谢任何帮助。提前致谢!

Here is my code:

这是我的代码:

//  OptionsViewController.h

#import <UIKit/UIKit.h>

@interface OptionsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSArray *themeDataArray;
    NSArray *levelDataArray;
    NSIndexPath *selectedRowIndex;
}

@property (weak, nonatomic) NSArray *themeDataArray;
@property (weak, nonatomic) NSArray *levelDataArray;
@property (weak, nonatomic) NSIndexPath *selectedRowIndex;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end


//  OptionsViewController.m

#import "OptionsViewController.h"
@interface OptionsViewController ()
@end

@implementation OptionsViewController

- (void)viewDidLoad
{
    themeDataArray = [[NSArray alloc] initWithObjects:@"Classic",@"Animals",@"Dinosaurs",@"Cars", nil];
    levelDataArray = [[NSArray alloc] initWithObjects:@"Easy",@"Medium",@"Hard", nil];

    //set default rows on startup
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView selectRowAtIndexPath:indexPath animated:YES  scrollPosition:UITableViewScrollPositionTop];

    NSIndexPath *indexPath2=[NSIndexPath indexPathForRow:0 inSection:1];
    [self.tableView selectRowAtIndexPath:indexPath2 animated:YES  scrollPosition:UITableViewScrollPositionNone];

    [super viewDidLoad];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection (NSInteger)section
{
    //For each section, return header label
    if(section == 0) return @"Themes";
    if(section == 1) return @"Difficulty Level";
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //For each section, return number of rows in section
    if(section == 0) return [themeDataArray count];
    if(section == 1) return [levelDataArray count];
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell1 = nil;

    //recycles cells to save memory
    cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1"];

    if (cell1 == nil)
    {
        cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
    }

    cell1.textLabel.text=[themeDataArray objectAtIndex:indexPath.row];
    return cell1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView reloadData];
    switch (indexPath.section)
    {
        case 0:
        {
            if (self.selectedRowIndex)
            {
                UITableViewCell *deselectCell = [tableView cellForRowAtIndexPath:self.selectedRowIndex];
                deselectCell.highlighted = FALSE;
                [tableView reloadData];
            }

            //select new row and assign new indexPath
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
            newCell.highlighted = TRUE;

            self.selectedRowIndex = indexPath;
            break;
        }
        case 1:
        {
            if (self.selectedRowIndex)
            {
                UITableViewCell *deselectCell = [tableView cellForRowAtIndexPath:self.selectedRowIndex];
                deselectCell.highlighted = FALSE;
            }

            //select new row and assign new indexPath
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
            newCell.highlighted = TRUE;

            self.selectedRowIndex = indexPath;
            break;
        }
        default:
            break;
    }
}

@end

回答by John Sauer

So, you'd like no more than 1 row to be selected at a time in each section? Then a solution is to, whenever the user touches a row, deselect any selected rows in the same section.

那么,您希望每个部分一次选择的行不超过 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 ;
}

回答by Gligor

Here's the Swift 3 equivalent of John Sauer's answer above (in case anyone else stumbles upon this question like I just did):

这是 Swift 3 相当于上面John Sauer的回答(以防其他人像我一样偶然发现这个问题):

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    if let selectedIndexPaths = tableView.indexPathsForSelectedRows {
        for selectedIndexPath in selectedIndexPaths {
            if selectedIndexPath.section == indexPath.section {
                tableView.deselectRow(at: selectedIndexPath, animated: true)
            }
        }
    }
    return indexPath
}

回答by Linearhw

I think this is more Swifty styled code of the answer above.

我认为这是上述答案的更多 Swifty 风格的代码。

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    tableView.indexPathsForSelectedRows?
        .filter { ##代码##.section == indexPath.section }
        .forEach { tableView.deselectRow(at: ##代码##, animated: true) }
    return indexPath
}