2011-03-18 39 views
0
if ([stararray count] > 0) { 
     for (int i = 0;i < [stararray count]; i++) { 
      NSString *starstr =[stararray objectAtIndex:i]; 
      NSInteger starinteger = [starstr integerValue]; 
      btn = [[UIButton alloc] initWithFrame:kLabelRect2]; 
      btn.tag =starinteger - 1; 
      NSLog(@"btnID1:%d",btn.tag); 
      [btn setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal]; 
      [btn addTarget:self action:@selector(starClicked:) 
      forControlEvents:UIControlEventTouchUpInside]; 

      [cell.contentView addSubview:btn]; 
      btn.hidden = inPseudoEditMode; 
     } 

     for (int i = 0;i < [nostararray count]; i++) { 
     NSString *starstr =[nostararray objectAtIndex:i]; 
     NSInteger starinteger = [starstr integerValue]; 
     btn = [[UIButton alloc] initWithFrame:kLabelRect2]; 
      btn.tag =starinteger - 1; 
      NSLog(@"btnID2:%d",btn.tag); 
      [btn setImage:[UIImage imageNamed:@"dot.png"] forState:UIControlStateNormal]; 
      [btn addTarget:self action:@selector(starClicked:) forControlEvents:UIControlEventTouchUpInside]; 
            [cell.contentView addSubview:btn]; 
btn.hidden = inPseudoEditMode; 
     } 

     //stararray = [[NSMutableArray alloc]init]; 
    } 
    else { 
     btn = [[UIButton alloc] initWithFrame:kLabelRect2]; 
     btn.tag =indexPath.row; 
     NSLog(@"btnID2:%d",btn.tag); 
     [btn setImage:[UIImage imageNamed:@"dot.png"] forState:UIControlStateNormal]; 
     [btn addTarget:self action:@selector(starClicked:) 
     forControlEvents:UIControlEventTouchUpInside]; 

     [cell.contentView addSubview:btn]; 
     btn.hidden = inPseudoEditMode; 
    } 
+0

如存在e是兩行,然後當用戶單擊第一行上的第一個按鈕時,然後在第二個按鈕中進行更改。請找一些解決方案 – Harshal 2011-03-18 07:08:01

+0

你可以發佈你的最新代碼嗎? – visakh7 2011-03-18 07:21:24

+0

看看這個問題,這是最新的代碼 – Harshal 2011-03-18 07:25:30

回答

2

您可以自定義UITableViewCell有點擊它時,一個按鈕執行烏爾必要的行動

更新

-(void)starClicked:(id)sender 
    { 

     UIButton *button = (UIButton *)sender; 
     NSLog(@"btnID:%d",button.tag); 
     if(button.tag == initialTag) { 
      [button setImage:[UIImage imageNamed:@"star.png" forState:UIControlStateNormal]]; 
      button.tag = newTag; 
     } 
     else if(button.tag == newTag) 
     { 
      [button setImage:[UIImage imageNamed:@"dot.png" forState:UIControlStateNormal]]; 
      button.tag = initialTag; 
     } 

     NSLog(@"btnID:%d",btn.tag); 

    } 

另一個選項

-(void)starClicked:(id)sender 
{ 

UIButton *button = (UIButton *)sender; 
button.selected = !button.selected; // toggle the selected property, 

    if (button.selected) 
    { 

// code to change image for selected state. 
    } 
    else 
    { 
// code to change image for normal state. 
    } 
} 
+0

@ 7KV7感謝它的工作 – Harshal 2011-03-18 07:36:29

+0

@ 7KV7嘿我如何帶回dot.png點擊圖片名稱star.png – Harshal 2011-03-18 09:31:33

+0

查看更新的答案。初始標籤是標籤,當你有點圖像和新標籤是當你有明星圖像 – visakh7 2011-03-18 09:38:05

0

您可以添加UIButton到你的表格單元格,這將切換當用戶按他們。

-(void) toggleStarButton:(id)sender 
{ 
    if([sender tag]== 0) 
    { 
     [sender setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal]; 
     [sender setTag:1]; 


    else if([sender tag]==1) 
    { 
     [sender setImage:[UIImage imageNamed:@"unStar.png"] forState:UIControlStateNormal]; 
     [sender setTag:0]; 

    } 
} 
+0

嘿,但它在選擇按鈕 – Harshal 2011-03-18 07:00:56

+0

時給出了一個問題什麼是問題? – visakh7 2011-03-18 07:03:30

+0

檢查我編輯我的答案。 – Jhaliya 2011-03-18 07:06:06