2012-09-06 2 views
0

UITableViewCell의 각 셀에 사용자 정의 버튼을 추가했습니다.이 버튼의 유형은 custom입니다.이 버튼에 이미지를 추가했습니다.이 버튼을 선택하면 이미지가 변경됩니다. 그러나 버튼의 이러한 이미지는 겹쳐 있습니다. 다음은 내 코드입니다.UITableVIewCell 오버랩 이미지의 사용자 정의 버튼

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

static NSString *CellIdentifier = @"CountryCell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; 


} 

// Configure the cell... 

cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
cell.textLabel.numberOfLines = 0; 
cell.selectionStyle = UITableViewCellSelectionStyleNone ; 
cell.textLabel.text = [[_listArray objectAtIndex:indexPath.row] wish]; 
cell.textLabel.textColor = [UIColor whiteColor]; 

CustomWishButton *newBtn = [CustomWishButton buttonWithType:UIButtonTypeCustom]; 
[newBtn setFrame:CGRectMake(280,5,35,34)]; 
cell.textLabel.font = [UIFont fontWithName:@"Times New Roman" size:21]; 
newBtn.customString = [[_listArray objectAtIndex:indexPath.row] wish] ; 
newBtn.customStatus = [[_listArray objectAtIndex:indexPath.row] status]; 
newBtn.imageName = [[_listArray objectAtIndex:indexPath.row]imageName]; 

[newBtn setImage:[UIImage imageNamed:newBtn.imageName] forState:UIControlStateNormal]; 
[newBtn addTarget:self action:@selector(changeStatus:) forControlEvents:UIControlEventTouchUpInside]; 

[cell addSubview:newBtn]; 

return cell; 
} 

-(void)changeStatus:(id)sender{ 

    CustomWishButton *resultButton= (CustomWishButton*)sender; 

if ([resultButton.imageName isEqualToString:@"cancel.png"]) { 

    NSString *wish = resultButton.customString; 

    NSLog(@"wish: %@",wish); 

    [sql updateData:wish:1:@"tick.png"]; 


} 
else{ 


    NSString *wish = resultButton.customString; 



    [sql updateData:wish:0:@"cancel.png"]; 
} 

_listArray = [sql getList]; 

[wishTableView reloadData]; 
} 

답변

0
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     NSString *CellIdentifier = [NSString stringWithFormat:@"CountryCell%d",indexPath.row]; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

      //CustomWishButton shouldn't add each and everytime! So why I kept inside. 
      CustomWishButton *newBtn = [CustomWishButton buttonWithType:UIButtonTypeCustom]; 
      [newBtn setFrame:CGRectMake(280,5,35,34)]; 
      cell.textLabel.font = [UIFont fontWithName:@"Times New Roman" size:21]; 
      newBtn.customString = [[_listArray objectAtIndex:indexPath.row] wish] ; 
      newBtn.customStatus = [[_listArray objectAtIndex:indexPath.row] status]; 
      newBtn.imageName = [[_listArray objectAtIndex:indexPath.row]imageName]; 

      [newBtn setImage:[UIImage imageNamed:newBtn.imageName] forState:UIControlStateNormal]; 
      [newBtn addTarget:self action:@selector(changeStatus:) forControlEvents:UIControlEventTouchUpInside]; 

      [cell addSubview:newBtn]; 
     } 

     // Configure the cell... 

     cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
     cell.textLabel.numberOfLines = 0; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone ; 
     cell.textLabel.text = [[_listArray objectAtIndex:indexPath.row] wish]; 
     cell.textLabel.textColor = [UIColor whiteColor]; 


     return cell; 
    } 

    //instead of (id) reference I make it your CustomWishButton reference 
    -(void)changeStatus:(CustomWishButton *)resultButton 
    { 
     if ([resultButton.imageName isEqualToString:@"cancel.png"]) 
     { 
      NSString *wish = resultButton.customString; 

      NSLog(@"wish: %@",wish); 

      [sql updateData:wish:1:@"tick.png"]; 
     } 
     else 
     { 
      NSString *wish = resultButton.customString; 
      [sql updateData:wish:0:@"cancel.png"]; 

     } 

     //Change image as per your condition 
     [resultButton setImage:[UIImage imageNamed:NewImageName] forState:UIControlStateNormal]; 

     _listArray = [sql getList]; 

     [wishTableView reloadData]; 
    }