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]; } }
- 在显示图层之前应用CGAffineTransform
- 我应该如何预加载所有图像并使用AFNetworking缓存它们?
- CosmicMind / Material SideMenu,如何更改viewController?
- 自定义信息窗口上的按钮未在ios中接收操作
- 为什么我无法通过使用“usedRectForTextContainer”来获取textContainer的矩形
- 如何使用AutoLayoutanimation滑动UIView?
- iTunes在应用程序购买
- CAShapeLayer上的CABasicAnimation不能用于path更改
- Facebook iOS SDK是否要求用户每次使用应用程序时进行身份validation?