UITableVIewCell 重叠图像中的自定义按钮

Posted

技术标签:

【中文标题】UITableVIewCell 重叠图像中的自定义按钮【英文标题】:Custom button in UITableVIewCell overlapping images 【发布时间】:2012-09-06 07:23:01 【问题描述】:

我在 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];

【问题讨论】:

【参考方案1】:
        - (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];
    

【讨论】:

以上是关于UITableVIewCell 重叠图像中的自定义按钮的主要内容,如果未能解决你的问题,请参考以下文章

ios7中的UITableViewCell现在左右有间隙

如何从 UITableViewCell 中的自定义 UIButton 中删除触摸延迟

无法查看 UITableViewCell 中的自定义标签

带有 UIImageView 的自定义 UITableViewCell 不显示图像

UITableViewCell : ContentView 中的 UIImageView 与 iPhone SDK 中的图像重叠。

带有用于突出显示的图像的自定义 UITableViewCell