2013-08-19 29 views
1

她tableAllconacts是我的NSMutableArray無法更改按鈕的背景圖像中的UITableView如果一旦設定

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

{ 

    static NSString *CellIdentifier = @"Cell"; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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


     btnChk =[UIButton buttonWithType:UIButtonTypeCustom]; 
     btnChk.frame =CGRectMake(280, 4, 32, 32); 
     btnChk.backgroundColor =[UIColor clearColor]; 
     [btnChk addTarget:self action:@selector(btnCheckmarkClicked:) forControlEvents:UIControlEventTouchUpInside]; 
     btnChk.tag=indexPath.row; 


     UIImageView *separator=[[UIImageView alloc ]initWithFrame:CGRectMake(0, 44, 320, 1)]; 
     separator.image=[UIImage imageNamed:@"divider.png"]; 
     [cell.contentView addSubview:separator]; 

    } 

    cell.textLabel.font = [UIFont systemFontOfSize:18]; 
    cell.textLabel.textColor =[UIColor whiteColor]; 
    cell.textLabel.text = [appdeleObj.arrDriverName objectAtIndex:indexPath.row]; 




    NSString *strValue=[appdeleObj.arrDriverName objectAtIndex:indexPath.row]; 

    NSLog(@"%@",strValue); 

     if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"Checked"]isEqualToString:strValue]) 

     { 

      if ([[NSUserDefaults standardUserDefaults ] boolForKey:@"showChkmark"]) 

      { 

     [btnChk setBackgroundImage:[UIImage imageNamed:@"radio_on.png"] forState:UIControlStateNormal];    

     } 
      else 
      { 

       [btnChk setBackgroundImage:[UIImage imageNamed:@"radio_off.png"] forState:UIControlStateNormal]; 

      } 
     } 
     else 

     { 
      NSLog(@"%@",btnChk.currentBackgroundImage); 
      [btnChk setBackgroundImage:[UIImage imageNamed:@"radio_off.png"] forState:UIControlStateNormal]; 

     } 
    [cell.contentView addSubview:btnChk]; 
    return cell; 

} 

按鈕操作的代碼更改背景圖片

-(void)btnCheckmarkClicked: (UIButton *)sender 
{ 

    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tableAllContacts]; 
    NSIndexPath *indexPath = [tableAllContacts indexPathForRowAtPoint:touchPoint]; 

// No need to use tag sender will keep the reference of clicked button 
    UIButton *button = (UIButton *)sender; 

    appdeleObj.strCheckUnchek =[appdeleObj.arrDriverName objectAtIndex:indexPath.row]; 

    if ([button.currentBackgroundImage isEqual:[UIImage imageNamed:@"radio_on.png"]]) 
    { 
     [[NSUserDefaults standardUserDefaults] setValue:@"radio_off.png" forKey:@"Checked"]; 
    // [button setBackgroundImage:nil forState:UIControlStateNormal]; 
     [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"showChkmark"]; 

    } 
    else 
    { 
     [[NSUserDefaults standardUserDefaults] setValue:appdeleObj.strCheckUnchek forKey:@"Checked"]; 
    // [button setBackgroundImage:[UIImage imageNamed:@"radio_on.png"] forState:UIControlStateNormal]; 
     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"showChkmark"]; 


    } 

    [tableAllContacts reloadData]; 

} 
+0

首先檢查它正在改變的NSUserDefaults值或ont –

+0

nsuserdefaults中的值完全改變,但當上述所有檢查工作正常時,設置的背景圖像未更新 –

+0

ok然後刪除單元格的狀態= =無] –

回答

0
  1. 線是在您發佈的代碼中發表了評論。

  2. 您正在該方法內部創建按鈕。

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

這將始終繪製按鈕重新加載表。我的建議是嘗試在上述方法之外創建按鈕,並在方法內設置標籤。

0

試試這個代碼:

-(void)btnCheckmarkClicked: (UIButton *)sender 
    { 

     CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tableAllContacts]; 
     NSIndexPath *indexPath = [tableAllContacts indexPathForRowAtPoint:touchPoint]; 

    // No need to use tag sender will keep the reference of clicked button 
     UIButton *button = (UIButton *)sender; 

     appdeleObj.strCheckUnchek =[appdeleObj.arrDriverName objectAtIndex:indexPath.row]; 

     if ([button.currentBackgroundImage isEqual:[UIImage imageNamed:@"radio_on.png"]]) 
     { 
//Update the BackGround Image When you click the button 
    [button setBackgroundImage:[UIImage imageNamed:@"radio_off.png"] forState:UIControlStateNormal]; 
      [[NSUserDefaults standardUserDefaults] setValue:@"radio_off.png" forKey:@"Checked"]; 
     // [button setBackgroundImage:nil forState:UIControlStateNormal]; 
      [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"showChkmark"]; 

     } 
     else 
     { 

    [button setBackgroundImage:[UIImage imageNamed:@"radio_on.png"] forState:UIControlStateNormal]; 
      [[NSUserDefaults standardUserDefaults] setValue:appdeleObj.strCheckUnchek forKey:@"Checked"]; 
     // [button setBackgroundImage:[UIImage imageNamed:@"radio_on.png"] forState:UIControlStateNormal]; 
      [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"showChkmark"]; 


     } 

     [tableAllContacts reloadData]; 

    } 
0

它可以幫助你。

if(cell == nil)條件寫這個。

btnChk =[UIButton buttonWithType:UIButtonTypeCustom]; 
btnChk.frame =CGRectMake(280, 4, 32, 32); 
btnChk.backgroundColor =[UIColor clearColor]; 
[btnChk addTarget:self action:@selector(btnCheckmarkClicked:) forControlEvents:UIControlEventTouchUpInside]; 
btnChk.tag=indexPath.row; 
[cell.contentView addSubview:btnChk]; //added this line to your code. 

輸出側,如果條件得到相同的按鈕像這樣使用標籤

UIButton *btn2 = (UIButton *)[cell.contentView viewWithTag:indexPath.row]; 

現在這個按鈕應用圖像。

0

而不是這樣做,你爲什麼不爲下面的按鈕的不同狀態設置兩個背景圖像?

[button setBackgroundImage:[UIImage imageNamed:@"radio_on.png"] 
        forState:UIControlStateSelected]; 
[button setBackgroundImage:[UIImage imageNamed:@"radio_off.png"] 
        forState:UIControlStateNormal]; 

然後你可以在反轉的-(void)btnCheckmarkClicked: (UIButton *)senderbutton.selected的價值;那麼該按鈕將自動爲您更新其backgroundImage。

button.selected = !button.selected; 

希望這會有所幫助。