ios - 如何在表格 View 单元格中添加复选框?

标签 ios objective-c iphone xcode

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

我的代码:

- (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 {

     }
}

提前致谢, 阿鲁尔

最佳答案

尝试修改下面的 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;

关于ios - 如何在表格 View 单元格中添加复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26688811/

相关文章:

iphone - 如果我转换 View 框架, subview 框架如何变化?

iphone - 自定义 map 的布局

iphone - 移动 safari 和 iOS 应用程序中的唯一标识符

ios - 更改 UIAlertView 键盘的类型

ios - uitableview 默认图像显示不同大小?

iOS Email Field Data 无需打开 Apple Mail 直接发送至电子邮件

iphone - 消除 UIImage imageNamed : FUD

ios - UICollection 的 IndexPath 到底是什么

iphone - 是否可以有多个 iAd 实例?

ios - 更快地构建用于测试的应用程序