2014-11-01 94 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

相關問題