UItableView使用UIButton替换复选标记或使用自定义CheckImage进行多选

时间:2015-08-21 05:30:52

标签: ios xcode uitableview uibutton

我已经问了一个问题UItableView Replacing Checkmarks with UIButton

但到现在还没有解决方案,任何人都会提供解决方案? 1.将此按钮设为单元格可选 要么 2.想要使用我的自定义图像更改默认选中标记

但功能需要正常工作

提前致谢

2 个答案:

答案 0 :(得分:0)

好的,这是一个完整的自定义,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];
}

答案 1 :(得分:0)

你可以试试这个: -

NSMutableArray * chechBoxList;

$('#dataTable').dataTable().fnAdjustColumnSizing(false);
在cellForRowAtIndexPath中

: -

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