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

时间:2013-01-27 22:59:13

标签: xcode tableview

我有一个包含2个部分的tableview。我希望每个部分只选择一行。现在我让它选择一行,但它适用于整个表格。最终,我想要选择2行,但每个SECTION只需要一行。我已经坚持了几天,无法弄明白。这可能吗??我是Xcode的新手,所以任何帮助都会非常感激。提前谢谢!

这是我的代码:

//  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

3 个答案:

答案 0 :(得分:25)

那么,你想在每个部分中一次选择不超过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 ;
}

答案 1 :(得分:5)

这里的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
}

答案 2 :(得分:0)

我认为这是上面答案的Swifty风格的代码。

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