表格复选框仅适用于一个部分目标-c

时间:2012-08-08 12:03:34

标签: objective-c ios xcode uitableview

我有一个包含3个部分和不同行的uiTableView,我想在第三部分添加复选框,

我创建自定义单元格并且我链接img和标签

像这张照片一样:

![在此处输入图片说明] [1]

我的自定义单元格代码是:

.h

: UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *checkBox;
@property (strong, nonatomic) IBOutlet UILabel *absenceCode;

@end

的.m

@synthesize checkBox;
@synthesize absenceCode;

- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
    // Initialization code
      checkBox.image = [UIImage imageNamed:@"emptycheck-box.png"];
}
return self;
}

@end

和UITableViewController的代码 viewDidLoad中

- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];


NSString *staKey = @"Start";
NSString *endKey = @"End";
NSString *absKey= @"Absence";

[contents setObject:[NSArray arrayWithObjects:@"Time: 08:00 Date: Fri,3 Aug, 2012", nil] forKey:staKey];
[contents setObject:[NSArray arrayWithObjects:@"Time: 17:57 Date: Fri,3 Aug, 2012", nil] forKey:endKey];
[contents setObject:[NSArray arrayWithObjects:@"Red",@"Black",@"Blue", nil] forKey:absKey];


[keys addObject:staKey];
[keys addObject:endKey];
[keys addObject:absKey];


[self setSectionKeys:keys];
[self setSectionContents:contents];

}

的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];

CheckBoxTableViewCell *cell = (CheckBoxTableViewCell*)[tableView 
dequeueReusableCellWithIdentifier:@"CheckBoxTableViewCell"];

        cell.imageView.image=[UIImage imageNamed:@"emptycheck-box.png"];




cell.checkBox.image = image;
cell.absenceCode.text =@"Redddd";
cell.text =contentForThisRow;
return cell;

 }
你能帮我吗? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

以下内容应该做你想做的事。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]]; 
    NSArray *contents = [[self sectionContents] objectForKey:key]; 
    NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]]; 

    if (indexPath.section != 2) {
            //Set up default cell here.
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
            cell.textLabel.text =contentForThisRow;
            return cell;
    }
    else {
            CheckBoxTableViewCell *cell = (CheckBoxTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"CheckBoxTableViewCell"];

            cell.imageView.image=[UIImage imageNamed:@"emptycheck-box.png"];
            cell.checkBox.image = image;
            cell.absenceCode.text =@"Redddd";
            return cell;
    }
}