2014-11-01 97 views
0

我创建自定义复选框在我的表格视图时,当我选择行时间没有改变我的复选框。请帮助我如何添加表格视图单元格中的复选框?

我的代码:

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

+0

你在哪儿定义'checkButton''pLblCabType''pLblTariff'?以及为什么要从单元格视图中删除子视图,如果它已出列,然后为其分配值? – 2014-11-01 12:02:19

+0

我正在创建检查按钮,PLblCabType,PlblTariff进入单元格视图。 – 2014-11-01 12:07:29

+0

你已经创建了UITableViewCell的子类? – 2014-11-01 12:10:30

回答

0

您需要创建一个自定义单元格查看以适当的方式,并添加您的按钮和其他标签。有多种方式可以做到这一点。如果你正在使用故事板(如果你使用的话),请检查这tutorial如何制作原型单元格,如果你使用的是xib文件,请检查this

1

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

相关问题