0

我有一个包含 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;
}

@结尾

和 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;

 }

请你帮我提前谢谢!

4

1 回答 1

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;
    }
} 
于 2012-08-08T12:09:56.393 回答