我想在用户选择任何行时显示自定义复选标记并取消选中tableview单元格的图像,并且当插入新行时,单元格将加载uncheckmark图像。
一旦我检查了行,那么即使在用新数据重新加载表之后,我想要显示先前检查过的行,而不是未选中的新行。
如何在结尾处获取带有选中标记的所有行。
我试过,但我无法第一次加载自定义复选标记图像我可以在取消选择行时更改,但它也有一些问题,因为它不像我上面提到的那样一直工作。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [cardData.mArrRecipient count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * sCellIdentifier = @"cell";
NSLog(@"cellforrow");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sCellIdentifier] autorelease];
RecipientData *recipientData = [[cardData.mArrRecipient objectAtIndex:indexPath.row]autorelease];
cell.textLabel.text = recipientData.sRecipientFName;
}
cell.accessoryType=UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (IndexPath)
{
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_checkmark_red.png"]] autorelease];
[tableView cellForRowAtIndexPath:indexPath].accessoryView = imageView;
IndexPath=FALSE;
}
else
{
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_uncheckmark_red.png"]] autorelease];
[tableView cellForRowAtIndexPath:indexPath].accessoryView = imageView;
IndexPath=TRUE;
}
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([cardData.mArrRecipient count]>0)
{
[IBTblNewRecipientDetail reloadData];
}
}
任何人都能帮助我吗?
答案 0 :(得分:1)
这是教程 http://weheartgames.com/2009/06/simple-iphone-checkbox/
或使用此
UIButton * checkBox=[UIButton buttonWithType:UIButtonTypeCustom];
checkBox.tag=indexPath.row;
checkBox.frame=CGRectMake(270,15, 20, 20);
[cell.contentView addSubview:checkBox];
[checkBox setImage:[UIImage imageNamed:@"selectedBoxWith.png"] forState:UIControlStateNormal];
[checkBox addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
-(void)checkBoxClicked:(id)sender{
if(self.isChecked ==NO){
self.isChecked =YES;
[sender setImage:[UIImage imageNamed: @"selectedBoxWithTik.png"] forState:UIControlStateNormal];
}else
{
self.isChecked =NO;
[sender setImage:[UIImage imageNamed:@"selectedBoxWith.png"]forState:UIControlStateNormal];
}
}
and
-(void)checkBoxClicked:(id)sender{
UIButton *tappedButton = (UIButton*)sender;
if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"selectedBoxWith.png"]]) {
[sender setImage:[UIImage imageNamed: @"selectedBoxWithTik.png"] forState:UIControlStateNormal];
}
else {
[sender setImage:[UIImage imageNamed:@"selectedBoxWith.png"]forState:UIControlStateNormal];
}
}
答案 1 :(得分:0)
选择多行:
- (void)addTableView {
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake("whatever frame you want to set")
style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.timeSheetEntryTableView.allowsMultipleSelection = YES;
self.array = [[NSMutableArray alloc]init];
[self.view addSubview:self.tableView];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"Cell Identifier";
self.cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!self.cell) {
self.cell = [[Cell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
self.cell.accessoryType = UITableViewCellAccessoryNone;
}
self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([self.array containsObject:indexPath])
{
[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box_selected.png"]];
}
else
{
[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box.png"]];
}
return self.cell;
}
//确实选择并取消选择我使用了自定义单元格
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.cell = [tableView cellForRowAtIndexPath:indexPath];
[self.array addObject:indexPath];
[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box_selected.png"]];
}
- (void)tableView:(UITableView *)tableView
didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
self.cell = [tableView cellForRowAtIndexPath:indexPath];
[self.array removeObject:indexPath];
[self.cell.checkImage setImage:[UIImage imageNamed:@"check_box.png"]];
}