Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode tableview how to select ONE row in EACH section

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!

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
like image 288
mablecable Avatar asked Jan 27 '13 22:01

mablecable


People also ask

How do you get the IndexPath row when a button in a cell is tapped?

You can walk up the view hierarchy to find the containing table view cell and the containing table view. Then you can ask the table view for the cell's index path. Then use it to find the index path like this: func addButtonTapped(_ sender: Any) { guard let button = sender as?

How do you delete a row in Tableview?

When a user slides horizontally across a row the editing style of the Tabel View Cell is set to delete. When the delete button is pressed, the item is deleted in the array and also the row is deleted in the Table View. Build and run the project and swipe-to-delete a row from the Table View.


1 Answers

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

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
}
like image 132
Gligor Avatar answered Oct 10 '22 07:10

Gligor