UITableViewCell - 每个部分选中一行标记

时间:2014-08-21 08:47:53

标签: ios objective-c uitableview checkmark accessorytype

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

  1. 搜索距离

    • 250英尺

    • 1000英尺

    • 4000英尺

  2. MAP TYPE

    • 标准

    • 卫星

    • 混合

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

    #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
    

4 个答案:

答案 0 :(得分:1)

你有这个错误

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

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

这里for循环取消选择所有单元格。你还要检查if loop中的部分

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

 cell.tag = indexPath.section;

并且在for循环中,如果条件是

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

答案 1 :(得分:0)

您的代码中的问题是,您只需通过替换下面的方法检查第0部分的行,就可以解决您的问题。

     - (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];

    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 {
    return nil;
}
}

答案 2 :(得分:0)

目前我发现了以下错误

  1. 您不保存为地图类型选择的单元格。
  2. 检查单元格的方式不正确。
  3. 在切换中,您不必在break声明
  4. 之后使用return
  5. case kPAWSettingsTableViewNumberOfSections:中,您应该返回kPAWSettingsTableViewNumberOfSections
  6. 前两个缺陷导致许多样板代码。我认为您应该为每个被选为NSIndexPath的部分存储@property。您可以在tableView:didSelectRowAtIndexPath:

    中执行操作,而不是取消选中所有内容并检查已点按
    1. 确定应选择哪一个。
    2. 存储selectedIndexPath
    3. 的本地旧值
    4. selectedIndexPath改为新的。
    5. 重新加载两行。
    6. 然后您必须在tableView:cellForRowAtIndexPath:中添加以下代码:

      if (indexPath.row == self.selectedIndexPath.row && indexPath.section == self.selectedIndexPath.section) {
          // Change to checkmark.
      } else { 
         // Change to not checked.
      }
      

      我在自己的项目中检查单元格的方式非常相似。我希望下面的代码也能帮助您了解我解决问题的方法:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          if (/*condition*/) {
              [...]
          }
          else if (/*checkmark section*/) {
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckmarkCellId forIndexPath:indexPath];
              cell.textLabel.text = [self labelForRow:indexPath.row];
              cell.accessoryType = (indexPath.row == [self defaultAgglomerationIndex]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
              cell.imageView.image = (indexPath.row == [self selectedAgglomerationIndex]) ? [UIImage imageNamed:@"icoMap"] : [UIImage imageNamed:@"region"];
              return cell;
          }
          else if (/*Another condition */) {
              [...]
          }
          return nil;
      }
      

      tableView:didSelectRowAtIndexPath:中的基本代码(我重新加载整个部分)

      self.selectedIndex = indexPath;
      NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
      [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
      

答案 3 :(得分:0)

试试这个,对我来说工作得很好:

- 变量:

@property NSInteger checkedCellRow;
@property NSInteger checkedCellSection;

-functions:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (checkedCellSection == indexPath.section)
{
if (checkedCellRow == indexPath.row)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
checkedCellRow = indexPath.row;
checkedCellSection = indexPath.section;
[myTable reloadData];
}