0

我在 iPhone 中有自定义单元格,其中有一个按钮和图像视图我想在单击按钮时更改图像视图的图像这是我的按钮代码

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

     imageDC *objMenu = [dataArray objectAtIndex:sender.tag];
    GET_DBHANDLER
    [dbHandler performSelector:@selector(like_image:) withObject: objMenu.image_id];


}
4

5 回答 5

0

试试下面的代码

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

    static NSString *CellIdentifier = @"yourCellName";

    UITableViewCell *cell=(UITableViewCell*)[[sender superview] superview];

    UITableView *tableView=(UITableView*)[cell superview];
    NSIndexPath *path=[tableView indexPathForCell:cell];


    yourCellName *selectedCell = (yourCellName*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    [selectedCell.imageView.image setImage:[UIImage imageNamed:@"yourImageName.png"]]; 
}
于 2013-07-30T06:09:46.320 回答
0

以下步骤将起作用:-

  1. 标记您的图像视图。
  2. 在 cellForRowatIndexpath 中使用此代码

    NSArray *nib;
    static NSString *cellIdentifier= @"cell";
    
    UITableViewCell *theCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    
    if(theCell== nil)
    {
        {
            nib = [[NSBundle mainBundle] loadNibNamed:<Your cellNibName> owner:self options:nil]; 
        }
        theCell = [nib objectAtIndex:0];
        theCell.selectionStyle = UITableViewCellSelectionStyleNone;
        // theCell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    
    UIImageView *imageView=(UIImageView*)[[theCell contentView] viewWithTag:<Your tag>];
    

    //用imageView做你想做的事

  3. 你得到了图像视图参考做你想做的事。

于 2013-07-30T06:02:50.447 回答
0

要找出您的确切索引的方法,UITableViewCell请使用以下内容:-

CGPoint point = [sender convertPoint:CGPointZero toView:_iTableView];
NSIndexPath *indexPath = [_iTableView indexPathForRowAtPoint:point];

你会得到你的indexpath。从这里你可以识别你的UIImageView,希望你给他们标签,然后改变图像。

于 2013-07-30T05:57:43.910 回答
0

首先给你的 imageView 提供 tagValue ,就像你给你的 imageView 一样 5。然后在 buttonClick 上访问它。

-(void)btnLikePressed:(UIButton *)sender
{
    UITableViewCell *cell=(UITableViewCell*)[sender superview];

    UIImageView*imageView=(UIImageView*)[cell viewWithTag:5];

    //Now do whatEver you want to do with your imageview
}

编辑

如果您将 imageView 添加为

[cell.contentView addSubview:imageView];  

然后像这样获取您的 tableView 单元格

UITableViewCell *cell=(UITableViewCell*)[[sender superview] superview];
于 2013-07-30T06:12:37.157 回答
0

在 UITableViewDataSource 函数中

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

在设置单元格时,只需将标签添加到您的按钮和 imageView 中。

cell.yourBtn.tag   = indexPath.row;
cell.imageView.tag = indexPath.row + 1000(any number);

现在在你的 UITableViewController 类

现在,当用户单击按钮时,您将获得实际上是您的行号和 imageView(btn tag + 1000) 标记的按钮标记,并且您可以使用相同的信息获取 imageView。

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

    NSInteger btnTag = [sender tag];
    UIImageView *imageView = [yourTableView viewWithTag:btnTag + 1000];
    // here your code by using imageView    
}   
于 2013-07-30T06:05:50.257 回答