繁体   English   中英

如何在表格视图单元格中添加复选框?

[英]How to Add Check Box in Table View Cell?

当我选择当时未更改复选框的行时,我在表格视图中创建了自定义复选框。 请帮我

我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

@try {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


            checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [checkButton setFrame:CGRectMake(5, 5, 20, 20)];
            [cell addSubview:checkButton];

            pLblCabType = [[UILabel alloc]initWithFrame:CGRectMake(30, 5, 200, 20)];
            [cell addSubview:pLblCabType];

            pLblTariff = [[UILabel alloc]initWithFrame:CGRectMake(240, 5, 80, 20)];
            [cell addSubview:pLblTariff];

    } 



        [checkButton setImage:[UIImage imageNamed:[pArrImgBullet objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
        pLblCabType.text = @"Arul";
        pLblTariff.text = @"Rs.1250";


    return cell;

}
@catch (NSException *exception) {

    NSLog(@"Exception Error is  %@",exception);
}
@finally {

   }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

@try {




        [checkButton setImage:[UIImage imageNamed:@"Bullet_Select.png"] forState:UIControlStateSelected];
        [pArrImgBullet replaceObjectAtIndex:indexPath.row withObject:@"Bullet_Select.png"];


    }

}
@catch (NSException *exception) {
    NSLog(@"Exception Error is %@",exception);

}
@finally {

     }
}

在此先感谢,S.Arul

尝试修改下面的cellForRowAtIndexPath方法:-

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"CellWithSwitch"];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellWithSwitch"] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:14];

        cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

return cell;

您将需要以正确的方式创建自定义单元格视图,并向其添加按钮和其他标签。 有多种方法可以做到这一点。 如果使用情节提要,请查看本教程,以了解如何制作原型单元(如果希望),如果使用xib文件,则请参见教程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM