ios - 在 UITableview 中进行多项选择时遇到问题

标签 ios objective-c uitableview

我一直在尝试选择像这样的单元格

同Checkbox一样从对象数组中选择元素

这是我的代码。请指导我..提前致谢

#import <UIKit/UIKit.h>

@interface CheckAllListViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
{
    IBOutlet UITableView *_myTable;
    NSMutableArray *_myArray;
    NSMutableArray *_selectedArray;
}
@property(nonatomic,retain) UITableView *myTable;
@property(nonatomic,retain)NSMutableArray *myArray;
@property(nonatomic,retain)NSMutableArray *selectedArray;
-(void)checkmarkAll;
-(void)UnChceckAll;

CheckAllListViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    _myArray=[[NSMutableArray alloc]initWithObjects:@"Check All",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10" ,nil];
    _selectedArray=[[NSMutableArray alloc]init];
    // Do any additional setup after loading the view from its nib.
}

#pragma mark -UITableViewDelegates & Datasource

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryNone) 
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.selectedArray addObject:[self.myArray objectAtIndex:indexPath.row]];

        //if 'all' row is selected then check all rows
        if([[self.myArray objectAtIndex:indexPath.row] isEqualToString:@"Check All"]) 
        {
            [self checkmarkAll];
        }
    } 
    else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) 
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.selectedArray removeObject:[self.myArray objectAtIndex:indexPath.row]];

        if([[self.myArray objectAtIndex:indexPath.row] isEqualToString:@"Check All"]) 
        {
            [self UnChceckAll];
            [self.selectedArray removeAllObjects];
        }
        else if([self.selectedArray containsObject:@"Check All"]) 
        {
            [self checkmarkAll];
            [self.selectedArray removeAllObjects];

            cell.accessoryType = UITableViewCellAccessoryNone;
            [self.selectedArray addObject:[self.myArray objectAtIndex:indexPath.row]];
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [_myTable dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if([self.selectedArray count]) 
    {

        if([self.selectedArray containsObject:[self.myArray objectAtIndex:indexPath.row]] || [self.selectedArray containsObject:@"Check All"]) 
        {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } 
        else 
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

    }
    cell.textLabel.text=[_myArray objectAtIndex:indexPath.row];
    // [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _myArray.count;
}

-(void)checkmarkAll
{
    for (int i=0;i<[self.myArray count] ; i++)
    {
        NSIndexPath *myIdx=[NSIndexPath indexPathForRow:i inSection:0];
        UITableViewCell *cell=[self.myTable cellForRowAtIndexPath:myIdx];
        cell.accessoryType=UITableViewCellAccessoryCheckmark;
    }
}

-(void)UnChceckAll
{
    for(int i=0;i<[self.myArray count];i++) 
    {

        NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
        UITableViewCell *cell = [self.myTable cellForRowAtIndexPath:myIndexPath];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

我用过这个教程 - http://iphonediscoveries.blogspot.in/2013/10/creating-inclusive-list-in-uitableview.html

我想实现: 1 为用户添加一个选项,以便在选择“全部”时能够选择所有行。

2当检查“全部”时,我想要所有的行检查标记,并且在“全部”未选中时将删除所有检查标记。

3 当'all'被选中,并且所有行都被选中时,如果此时点击了一行,那么所有其他行中的选中标记应该被删除(包括'all')并且只有该行应该包含选中标记. 4 调用和滚动 UITableview 后不应忘记选择

更新

-我的以下代码让我检查所有功能并取消选中所有功能。 假设我现在通过选择全部检查选择了所有行,如果我随机选择任何行应该从当前选定的元素中删除勾号,并且还应该从第一个元素中删除勾号,即@"Check All"
-取消选中功能相同

最佳答案

不确定我是否明白问题所在,但我会更改 checkAll 和 uncheckAll 方法来更改 selectedArray 并重新加载 tableview:

-(void)checkmarkAll
{
    self.selectedArray = [NSMutableArray arrayWithArray:self.myArray];
    [self.tableView reloadData];
}

关于ios - 在 UITableview 中进行多项选择时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24238986/

相关文章:

iphone - MKMapView 不显示当前位置

iphone - NSMutableURLRequest setHTTPBody : crash

iphone - 协助 iOS 无线应用程序分发的库/服务

ios - 从 cloudkit swift 获取不同的值

ios - 表格单元格中的 UIswitch 重复问题

ios - 以编程方式添加带有自动布局的 UITableView

iphone - Storyboard 不显示 Xcode iOS 选项卡

objective-c - 评估字符串并作为 Objective-C 代码执行

iphone - 如何从现有图像和其上的字符串创建新图像

ios - UIList View 仅在单元格不在 View 中时显示它们