UITableViewCell - 勾选每部分一行 [英] UITableViewCell - check mark one row per section

查看:48
本文介绍了UITableViewCell - 勾选每部分一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个部分和 3 行,每个部分如下:

I have 2 sections and 3 rows each as follow:

  1. 搜索距离

  1. SEARCH DISTANCE

  • 250 英尺

  • 250 feet

1000 英尺

4000 英尺

地图类型

  • 标准

卫星

混合

我想为每个部分的一行设置一个复选标记,但我当前的代码将取消选中整个表格视图的所有可见单元格,并在选定的行留下复选标记.换句话说,我将为整个表格(2 个部分)设置一个复选标记.在这里,我发布了我的整个代码.我用谷歌搜索了很多,但似乎没有解决我的问题.任何人,请帮助更正我的代码.提前致谢.

I want to have one check mark for a row per section but my current code will uncheck all visible cells for the entire table view and leave one selected row with check mark. In other word, I will have one check mark for the entire table (2 sections). Here I posted my entire code. I have google'd many but there seems none to resolve my problem. Anyone, please help correct my code. Thanks in advance.

#import "PAWSettingsViewController.h"
#import "PAWAppDelegate.h"
#import <Parse/Parse.h>

@interface PAWSettingsViewController ()

- (NSString *)distanceLabelForCell:(NSIndexPath *)indexPath;
- (PAWLocationAccuracy)distanceForCell:(NSIndexPath *)indexPath;


- (NSString *)maptypeLabelForCell:(NSIndexPath *)indexPath;
- (PAWMaptypeSelect)maptypeForCell:(NSIndexPath *)indexPath;

@property (nonatomic, assign) CLLocationAccuracy filterDistance;

@end



typedef enum {
    kPAWSettingsTableViewDistance = 0,
    kPAWSettingsTableViewMaptype,
    kPAWSettingsTableViewNumberOfSections
} kPAWSettingsTableViewSections;


typedef enum {
    kPAWSettingsTableViewDistanceSection250FeetRow = 0,
    kPAWSettingsTableViewDistanceSection1000FeetRow,
    kPAWSettingsTableViewDistanceSection4000FeetRow,
    kPAWSettingsTableViewDistanceNumberOfRows
} kPAWSettingsTableViewDistanceSectionRows;

typedef enum {
    kPAWSettingsTableViewMaptypeSectionStandardRow = 0,
    kPAWSettingsTableViewMaptypeSectionSatelliteRow,
    kPAWSettingsTableViewMaptypeSectionHybridRow,
    kPAWSettingsTableViewMaptypeNumberOfRows
} kPAWSettingsTableViewMaptypeSectionRows;


@implementation PAWSettingsViewController

@synthesize tableView;
@synthesize filterDistance;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        PAWAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        self.filterDistance = appDelegate.filterDistance;
    }
    return self;
}


#pragma mark - Custom setters

// Always fault our filter distance through to the app delegate. We just cache it locally because it's used in the tableview's cells.
- (void)setFilterDistance:(CLLocationAccuracy)aFilterDistance {
    PAWAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    appDelegate.filterDistance = aFilterDistance;
    filterDistance = aFilterDistance;
}

#pragma mark - View lifecycle

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Private helper methods

- (NSString *)distanceLabelForCell:(NSIndexPath *)indexPath {
    NSString *cellText = nil;
    switch (indexPath.row) {
        case kPAWSettingsTableViewDistanceSection250FeetRow:
            cellText = @"250 feet";
            break;
        case kPAWSettingsTableViewDistanceSection1000FeetRow:
            cellText = @"1000 feet";
            break;
        case kPAWSettingsTableViewDistanceSection4000FeetRow:
            cellText = @"4000 feet";
            break;
        case kPAWSettingsTableViewDistanceNumberOfRows: // never reached.
        default:
            cellText = @"The universe";
            break;
    }
    return cellText;
}

- (PAWLocationAccuracy)distanceForCell:(NSIndexPath *)indexPath {
    PAWLocationAccuracy distance = 0.0;
    switch (indexPath.row) {
        case kPAWSettingsTableViewDistanceSection250FeetRow:
            distance = 250;
            break;
        case kPAWSettingsTableViewDistanceSection1000FeetRow:
            distance = 1000;
            break;
        case kPAWSettingsTableViewDistanceSection4000FeetRow:
            distance = 4000;
            break;
        case kPAWSettingsTableViewDistanceNumberOfRows: // never reached.
        default:
            distance = 10000 * kPAWFeetToMiles;
            break;
    }

    return distance;
}

- (NSString *)maptypeLabelForCell:(NSIndexPath *)indexPath {
    NSString *cellText = nil;
    switch (indexPath.row) {
        case kPAWSettingsTableViewMaptypeSectionStandardRow:
            cellText = @"Standard";
            break;
        case kPAWSettingsTableViewMaptypeSectionSatelliteRow:
            cellText = @"Satellite";
            break;
        case kPAWSettingsTableViewMaptypeSectionHybridRow:
            cellText = @"Hybrid";
            break;
        case kPAWSettingsTableViewMaptypeNumberOfRows: // never reached.
        default:
            cellText = @"?";
            break;
    }
    return cellText;
}

- (PAWMaptypeSelect)maptypeForCell:(NSIndexPath *)indexPath {
    PAWMaptypeSelect maptype = nil;
    switch (indexPath.row) {
        case kPAWSettingsTableViewMaptypeSectionStandardRow:
            maptype = @"Standard";
            break;
        case kPAWSettingsTableViewMaptypeSectionSatelliteRow:
            maptype = @"Satellite";
            break;
        case kPAWSettingsTableViewMaptypeSectionHybridRow:
            maptype = @"Hybrid";
            break;
        case kPAWSettingsTableViewMaptypeNumberOfRows: // never reached.
        default:
            maptype = nil;
            break;
    }

    return maptype;
}

#pragma mark - UINavigationBar-based actions

- (IBAction)done:(id)sender {
    [self.presentingViewController dismissModalViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource methods

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch ((kPAWSettingsTableViewSections)section) {
        case kPAWSettingsTableViewDistance:
            return kPAWSettingsTableViewDistanceNumberOfRows;
            break;

        case kPAWSettingsTableViewMaptype:
            return kPAWSettingsTableViewMaptypeNumberOfRows;
            break;

        case kPAWSettingsTableViewNumberOfSections:
            return 2;
            break;
    };
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"SettingsTableView";
    if (indexPath.section == kPAWSettingsTableViewDistance) {
        UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
        if ( cell == nil )
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];

        }

        // Configure the cell.
        cell.textLabel.text = [self distanceLabelForCell:indexPath];

        if (self.filterDistance == 0.0) {
            NSLog(@"We have a zero filter distance!");
        }

        PAWLocationAccuracy filterDistanceInFeet = self.filterDistance * ( 1 / kPAWFeetToMeters);
        PAWLocationAccuracy distanceForCell = [self distanceForCell:indexPath];
        if (abs(distanceForCell - filterDistanceInFeet) < 0.001 ) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }


        return cell;
    }

    else if (indexPath.section == kPAWSettingsTableViewMaptype){
        UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
        if ( cell == nil )
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
        }

        // Configure the cell.
        cell.textLabel.text = [self maptypeLabelForCell:indexPath];


        return cell;

    }


    else {
        return nil;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    switch ((kPAWSettingsTableViewSections)section) {
        case kPAWSettingsTableViewDistance:
            return @"Search Distance";
            break;

        case kPAWSettingsTableViewMaptype:
            return @"Map Type";
            break;

        case kPAWSettingsTableViewNumberOfSections:
            return @"";
            break;
    }
}

#pragma mark - UITableViewDelegate methods

// Called after the user changes the selection.
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    if (indexPath.section == kPAWSettingsTableViewDistance) {
        [aTableView deselectRowAtIndexPath:indexPath animated:YES];



        // if we were already selected, bail and save some work.
        UITableViewCell *selectedCell = [aTableView cellForRowAtIndexPath:indexPath];
        if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) {
            return;
        }


        // uncheck all visible cells.
        for (UITableViewCell *cell in [aTableView visibleCells]) {
            if (cell.accessoryType != UITableViewCellAccessoryNone) {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
        }


        selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;

        PAWLocationAccuracy distanceForCellInFeet = [self distanceForCell:indexPath];
        self.filterDistance = distanceForCellInFeet * kPAWFeetToMeters;
    }



    else if (indexPath.section == kPAWSettingsTableViewMaptype){
        [aTableView deselectRowAtIndexPath:indexPath animated:YES];

        // if we were already selected, bail and save some work.
        UITableViewCell *selectedCell = [aTableView cellForRowAtIndexPath:indexPath];
        if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) {
            return;
        }

        // uncheck all visible cells.
        for (UITableViewCell *cell in [aTableView visibleCells]) {
            if (cell.accessoryType != UITableViewCellAccessoryNone) {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
        }

        selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;

        PAWMaptypeSelect maptypeForCell = [self maptypeForCell:indexPath];

    }

}


@end

推荐答案

你有一个错误

//取消选中所有可见单元格.

// uncheck all visible cells.

    for (UITableViewCell *cell in [aTableView visibleCells]) {
        if (cell.accessoryType != UITableViewCellAccessoryNone) {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

这里 for 循环取消选择所有单元格.您还必须检查 if 循环中的部分

Here the for loop deselect all the cell. you have to check also the section in if loop

你可以像这样为你的单元格设置标签

you can set tag for your cell like this

 cell.tag = indexPath.section;

如果条件为,则在 for 循环中

and in for loop if condition is

    for (UITableViewCell *cell in [aTableView visibleCells]) {
        if (cell.accessoryType != UITableViewCellAccessoryNone && cell.tag == indexPath.section) {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }

这篇关于UITableViewCell - 勾选每部分一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆