如何添加表格视图单元格中的复选框?
问题描述:
我创建自定义复选框在我的表格视图时,当我选择行时间没有改变我的复选框。请帮助我如何添加表格视图单元格中的复选框?
我的代码:
- (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;
+0
感谢您的回复,但我需要单元格左侧的自定义项目符号类型复选框。 – 2014-11-01 11:56:16
你在哪儿定义'checkButton''pLblCabType''pLblTariff'?以及为什么要从单元格视图中删除子视图,如果它已出列,然后为其分配值? – 2014-11-01 12:02:19
我正在创建检查按钮,PLblCabType,PlblTariff进入单元格视图。 – 2014-11-01 12:07:29
你已经创建了UITableViewCell的子类? – 2014-11-01 12:10:30