我一直试图选择像
这样的细胞这是我的代码。请指导我..提前致谢
#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;
- (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
之后不应忘记选择 - 我的以下代码让我检查所有功能&amp;取消选中所有功能。
让我们说我现在选择全部检查选择所有行,如果我随机选择任何一行应从当前所选元素中删除勾选&amp;同样应该删除第一个元素的勾号,即@&#34;全部检查&#34;
- 取消选中功能
答案 0 :(得分:1)
不确定我是否得到了问题,但我会更改checkAll和uncheckAll方法来更改selectedArray并重新加载tableview:
-(void)checkmarkAll
{
self.selectedArray = [NSMutableArray arrayWithArray:self.myArray];
[self.tableView reloadData];
}