如何使用复选标记选择所有行作为默认值?

时间:2016-06-28 10:10:04

标签: ios objective-c uitableview

我想在视图出现时选择所有行,我在viewDidAppear中使用索引路径,但它只选择了一行而没有Checkmark 这是我的viewDidAppear方法:

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.topItem.title = @"Select Blood Donors";
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [_tblResult selectRowAtIndexPath:indexPath animated:YES  scrollPosition:UITableViewScrollPositionBottom];
}

我的表格查看方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdent = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];

    if(cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];

    cell.textLabel.text = [arrName objectAtIndex:indexPath.row];

    if([arSelectedRows containsObject:indexPath]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if(cell.accessoryType == UITableViewCellAccessoryNone) {

        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [arSelectedRows addObject:indexPath];

    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [arSelectedRows removeObject:indexPath];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    for(NSIndexPath *indexPath in arSelectedRows) {
        [selections addObject:[arrName objectAtIndex:indexPath.row]];
    }

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [arrName count];
}

6 个答案:

答案 0 :(得分:3)

我最终根据你的问题要求得到了输出。我试过这个超过3个小时。如果你用布尔值尝试这个,你就无法实现,因为你有多个复选框。如果你只有一个复选框,你可以轻松使用布尔值。

 #import "ViewController.h"

 @interface ViewController ()
 {
    NSMutableArray *arrProduct,*arrSelectDeSelectCheckMark;
 }

 @end

 @implementation SecondViewController

 @synthesize tblviewCheckMark;

- (void)viewDidLoad 
 {
     [super viewDidLoad];
     // Do any additional setup after loading the view from its nib.
     arrProduct = [[NSMutableArray alloc]initWithObjects:@"iOS",@"Android",@"Windows",@"Blackberry",@"Symbian",@"bada",nil];
     arrSelectDeSelectCheckMark = [[NSMutableArray alloc]init];
     for(int j=0;j<[arrProduct count];j++)
     {
         [arrSelectDeSelectCheckMark addObject:@"selected"];
     }
 }

- (void)didReceiveMemoryWarning 
{
   [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 #pragma mark - UITableViewDataSource Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return arrProduct.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *strCell = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
    if(cell==nil)
    {
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
    }
    if([[arrSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"selected"])
      cell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
       cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.text = [arrProduct objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   @try
   {
      CGPoint touchPoint = [cell convertPoint:CGPointZero toView:tblviewCheckMark];
      NSIndexPath *indexPath = [tblviewCheckMark indexPathForRowAtPoint:touchPoint];
      NSLog(@"%@",arrSelectDeSelectCheckMark);
      if([arrSelectDeSelectCheckMark count]==0)
      {
         for(int i=0; i<[arrProduct count]; i++)
         {
            [arrSelectDeSelectCheckMark addObject:@"select"];
         }
      }
      if([[arrSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"selected"])
      {
         cell.accessoryType = UITableViewCellAccessoryNone;
         [arrSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"deselected"];
      }
      else
      {
         cell.accessoryType = UITableViewCellAccessoryCheckmark;
         [arrSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"selected"];
      }
    }
    @catch (NSException *exception) {
     NSLog(@"The exception is-%@",exception);
    }
}

@end

上面的代码工作正常。首先选择视图加载后我们可以改变它。

答案 1 :(得分:0)

请参阅以下链接:

您可以在其中找到UItableview中的Mutliple行选择的示例代码以及进一步的操作:

https://developer.apple.com/library/ios/samplecode/TableMultiSelect/Introduction/Intro.html

答案 2 :(得分:0)

你可以直接给出

cell.accessoryType = UITableViewCellAccessoryCheckmark;

而不是

if([arSelectedRows containsObject:indexPath]) { 
cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} else { 
cell.accessoryType = UITableViewCellAccessoryNone; 
}

这将为所有单元格添加复选标记。然后,当用户点击didSelectRowAtIndexPath方法

中的单元格时,您需要处理它

答案 3 :(得分:0)

使用两个属性值和isSelected创建数据对象。您的数组名称是“arrName”。使用默认isSelected=YES添加数组中的所有对象。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [arrName objectAtIndex:[indexPath row]].isSelected=YES;
    [tableView reloadData];//or just row
}

didDeselectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [arrName objectAtIndex:[indexPath row]].isSelected=NO;
    [tableView reloadData];//or just row
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //initial code
    //addd this code
    if([data objectAtIndex:[indexPath row]].isSelected){
      //set checked 
    }else {
      //set unchecked 
    }
}

答案 4 :(得分:0)

我建议您使用NSMutableIndexSet来存储行选择状态。这样,您可以独立于行选择来管理复选标记的外观:

@property (strong,nonatomic) NSMutableIndexSet *checkedRows;

-(void)viewDidLoad {
    [super viewDidLoad];
    self.checkedRows = [NSMutableIndexSet new];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.checkedRows addIndexesInRange:NSMakeRange(0,arrName.count-1);
    [self.tableview reloadData];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSString *cellIdent = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];

    if(cell == nil)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];

    cell.textLabel.text = [arrName objectAtIndex:indexPath.row];

    if ([self.checkedRows containsIndex:indexPath.row]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;   
    }  

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCellAccessory accessory = UITableViewCellAccessoryNone;        

    if ([self.checkedRows containsIndex:indexPath.row]) {
        [self.checkedRows removeIndex:indexPath.row];
    } else {
        [self.checkedRows addIndex:indexPath.row];
        accessory = UITableViewCellAccessoryCheckmark;
    }

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = accessory;

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

答案 5 :(得分:-1)

您应该使用它来允许多项选择:

self.tableView.allowsMultipleSelection = YES;

然后,执行此操作:

for (int i = 0; i < [_tblResult numberOfRowsInSection:0]; i++) {
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:i inSection:0];
    [_tblResult selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

未经测试但应该可以使用。