UItableView用UIButton替代选中标记或用于多选的自定义CheckImage

我已经问了一个问题UItableView用UIButtonreplace复选标记

但到现在还没有解决的办法,谁都会提供我的解决scheme? 1.将此button设置为单元格可选,或者2.要更改默认选中标记与我的自定义图像

但function需要按照原样运行

提前致谢

好的,这是一个完整的自定义UITableViewCell

#import "NSHCellStuffTableViewCell.h" @interface NSHCellStuffTableViewCell () @end @implementation NSHCellStuffTableViewCell { UILabel * nameLabel; UILabel * mainLabel; UIButton * cellIcon; } - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self==nil) return nil; [self setSelectionStyle:UITableViewCellSelectionStyleNone]; nameLabel = [[UILabel alloc] init]; [nameLabel setTextColor:[UIColor blackColor]]; [nameLabel setBackgroundColor:[UIColor colorWithHue:32 saturation:100 brightness:63 alpha:1]]; [nameLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]]; [nameLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; [[self contentView] addSubview:nameLabel]; mainLabel = [[UILabel alloc] init]; [mainLabel setTextColor:[UIColor blackColor]]; [mainLabel setBackgroundColor:[UIColor colorWithHue:66 saturation:100 brightness:63 alpha:1]]; [mainLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]]; [mainLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; [[self contentView] addSubview:mainLabel]; cellIcon = [UIButton buttonWithType:UIButtonTypeCustom]; [cellIcon setImage:[UIImage imageNamed:@"THE_NAME_OF_YOUR_IMAGE_WHEN_CELL_IS_NOT_SELECTED"] forState:UIControlStateNormal]; [cellIcon setImage:[UIImage imageNamed:@"THE_NAME_OF_YOUR_IMAGE_WHEN_CELL_IS_SELECTED"] forState:UIControlStateSelected]; [cellIcon setTranslatesAutoresizingMaskIntoConstraints:NO]; [[self contentView] addSubview:mainLabel]; NSDictionary *views = NSDictionaryOfVariableBindings(nameLabel, mainLabel, cellIcon); NSDictionary * metrics = @{@"hello" : @13, @"goodBye" : @25}; [[self contentView] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hello-[cellIcon(goodBye)]-hello-[nameLabel]" options: 0 metrics:metrics views:views]]; [[self contentView] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hello-[cellIcon]-hello-[mainLabel]" options: 0 metrics:metrics views:views]]; [[self contentView ] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[cellIcon]|" options: 0 metrics:metrics views:views]]; [[self contentView ] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-hello-[nameLabel]-hello-[mainLabel]-hello-|" options: 0 metrics:metrics views:views]]; return self; } -(void)setNameText:(NSString *)nameText { _nameText = nameText; [nameLabel setText:_nameText]; } -(void)setMainText:(NSString *)mainText { _mainText = mainText; [mainLabel setText:_mainText]; } -(void)setCustomSelector:(BOOL)customSelector { _customSelector = customSelector; [cellIcon setSelected:_customSelector]; } @end 

NSHCellStuffTableViewCell.h

 #import <UIKit/UIKit.h> @interface NSHCellStuffTableViewCell : UITableViewCell @property (nonatomic) NSString *nameText; @property (nonatomic) NSString *mainText; @property (nonatomic) BOOL customSelector; @end 

所以,这是如何工作的

你必须导入“NSHCellStuffTableViewCell.h”到你的UIViewController

你必须像这样在你的ViewDidLoad中注册这个类:

 [tableView registerClass:[NSHCellStuffTableViewCell class] forCellReuseIdentifier:@"YOUR_CLASS_IDENTIFIER"]; 

那么,你这样做:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSHCellStuffTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSHLessonCellLargeIdentifier forIndexPath:indexPath]; [cell setCustomSelector:TRUE]; } 

你可以试试这个:

NSMutableArray * chechBoxList;

 chechBoxList=[[NSMutableArray alloc] init]; for (int i=0; i<CountOfTable; i++) { [chechBoxList addObject:@"0"]; } 

在cellForRowAtIndexPath中: –

 [cell.checkboxBtn addTarget:self action:@selector(CheckBoxSelection:) forControlEvents:UIControlEventTouchUpInside]; [cell.checkboxBtn setTag:indexPath.row]; if ([[chechBoxList objectAtIndex:indexPath.row] isEqualToString:@"0"]) { [cell.checkboxBtn setBackgroundImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal]; } else{ [cell.checkboxBtn setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal]; } button CheckBoxSelection:- -(void)CheckBoxSelection:(UIButton *)sender{ if ([sender isSelected]) { [chechBoxList replaceObjectAtIndex:sender.tag withObject:@"0"]; [self.tableViewUninvitedFriend reloadData]; [sender setSelected:NO]; } else { [chechBoxList replaceObjectAtIndex:sender.tag withObject:@"1"]; [self.tableViewUninvitedFriend reloadData]; [sender setSelected:YES]; } }