简体   繁体   English

如何在不重新加载的情况下更改UITableView didSelectRow上的UIButton背景图像

[英]How to change UIButton Background Image on UITableView didSelectRow without Reloading

I added a **UIButton** in UITableView Cell . 我在UITableView Cell中添加了**UIButton** And I wantto add a functionality on both clicks (UIButton and UITable - didSelectRow ). 我想在两次单击上都添加一个功能(UIButton和UITable - didSelectRow )。

On Button Click I am able to change the button background image by Sender. 在按钮上单击“我可以通过发件人更改按钮背景图像”。 The Code is look like this: 该代码如下所示:

-(void)tableButton_OnClick:(id)sender
{
    UIButton *btn=(UIButton *)sender;
    [btn setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
}

And the same functionality I want to add on UITable - didSelectRow without Reloading the table. 和我想在UITable - didSelectRow上添加的功能相同UITable - didSelectRow而无需重新加载表。

You have to make your own custom UITableViewCell with public @property UIButton . 您必须使用public @property UIButton制作自己的自定义UITableViewCell Then in didSelectRowAtIndexPath you may use something like this: 然后,在didSelectRowAtIndexPath您可以使用如下所示的内容:

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YourCell* yourCell = (YourCell*)[tableView cellForRowAtIndexPath:indexPath];
    UIButton* button = yourCell.button;
    [button setImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
}

STEPS - try this 步骤-试试这个

  1. while adding button on cell ,set the tag value to your button 在单元格上添加按钮时,将标签值设置为按钮

    eg : 例如:

    btn.tag=indexpath.row; btn.tag = indexpath.row; cell.tag=indexpath.row; cell.tag = indexpath.row;

  2. On didSelectRowAtIndexPath , Create UIButton based on tag value eg: didSelectRowAtIndexPath ,基于标签值创建UIButton,例如:

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { UITableViewCell *cell=(UITableViewCell *)[self.TableView viewWithTag:indexPath.row]; UIButton *btn=(UIButton *)[cell viewWithTag:indexPath.row]; [self tableButton_OnClick:btn]; } 
if ([[[myarry objectAtIndex:indexPath.row] objectForKey:@"key_name"] isEqualToString:@"1"])
   {
     [cell.button_my setBackgroundImage:[UIImage imageNamed:@"first.png"] forState:UIControlStateNormal];

   }

   els
{

 [cell.button_my setBackgroundImage:[UIImage imageNamed:@"first_other.png"] forState:UIControlStateNormal];


        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM