简体   繁体   中英

ios Custom multiple selection for tableview

I have a table that has three sections in it. There are 4 rows per section. I want one selection per section which makes for 3 selections for the whole table. I'm trying to do this using the following functions that I made which reset the current section and sets the new selection.

- (void)selectOneRow:(int)row inSection:(int)section
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];

    for(int i = 0; i < [[self.dataSource objectAtIndex:section] count]; ++i) {
        NSIndexPath *ip = [NSIndexPath indexPathForRow:i inSection:section];
        [self.table deselectRowAtIndexPath:ip animated:NO];
        //[[self.table cellForRowAtIndexPath:ip] setHighlighted:NO];
    }

    [self.table selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    //[[self.table cellForRowAtIndexPath:indexPath] setHighlighted:YES];

    [self disableInteractionForRow:row inSection:section];
}

- (void)disableInteractionForRow:(int)row inSection:(int)section
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];

    for(int i = 0; i < [[self.dataSource objectAtIndex:section] count]; ++i) {
        NSIndexPath *ip = [NSIndexPath indexPathForRow:i inSection:section];
        [[self.table cellForRowAtIndexPath:ip] setUserInteractionEnabled:YES];
    }

    [[self.table cellForRowAtIndexPath:indexPath] setUserInteractionEnabled:NO];
}

An example of selecting the first row of the first section is:

[self selectOneRow:0 inSection:0];

This works great the first time, but every time afterword it turns the row off, leaving the section blank. How can I fix this? Or am I going about this all wrong in the first place? Thanks for you help.

** EDIT adding an image **

在此处输入图片说明

It sounds like you're trying to make it too complicated. Instead of putting three choices in one table view with three sections, make three separate tables and three separate views.

For example, go to the settings app on the iPhone. Tap "Mail, Contacts, Calendars", and then scroll down to the options "Show", "Preview", and "Minimum Font Size" under the "Mail" section title. For each one of those settings, a new view is pushed with a new table view controller, and you can only select one of the options and then go back. That keeps the interface clean, not to mention your code.

Hopefully restructuring it like this will work better for you. I don't know if I can say that the way you're doing it now is wrong, but it's not consistent with how Apple structures their apps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM