0

我在我的 uitableviewcell 中添加了一个 uibutton 并尝试在录制它时更改其背景图像 这里是代码

-(void)downloadImage:(UIButton *)link
{
    UITableViewCell *cell = (UITableViewCell*)[link superview];

    UIButton *view = [[UIButton alloc]init];
    NSArray *subviews = [cell subviews];

    for (view in subviews)
    {
        if([view isKindOfClass:[UIButton class]])
        {
            view = (UIButton*)subviews;
            [view setBackgroundImage:[UIImage imageNamed:@"yellow"] forState:UIControlStateNormal];
        }
    }
...

但它不工作

如果我添加这一行

视图 = (UIButton*) 子视图;

我收到这个错误

线程 1:信号 SIGTRAP

如果没有这条线,什么都不会发生,知道出了什么问题吗?

4

1 回答 1

2

这取决于您如何将子视图添加到您的 UITableViewCell 中:

-(UITableViewCell)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = @"cellID";

    UITableViewCell *cell = [tableView dequeReusableCellForID:cellID];

    if(cell == nil)
    {
        ...

        myButton = [[UIButton alloc] initWithFrame....];
        [myButton addTarget:self selector:@selector(downloadImage:) forControlEvent:UIControlEventTouchUpInside];

        // -------------------------------------------------------
        // This is the important part here.
        // Usually, we add "myButton" to the cell's contentView
        // You will need to match this subview hierarchy
        // in your "downloadImage:" method later
        // -------------------------------------------------------

        [cell.contentView addSubview:myButton];
    }

    return cell;
}

-(void)downloadImage:(id)sender
{
    // -------------------------------------------------------
    // Here, "sender" is your original "myButton" being tapped
    //
    // Then "[sender superview]" would be the parent view of your
    // "myButton" subview, in this case the "contentView" of
    // your UITableViewCell above.
    //
    // Finally, "[[sender superview] superview]" would be the
    // parent view of the "contentView", i.e. your "cell"
    // -------------------------------------------------------

    UIButton *button = (UIButton *)sender;

    // button now references your "myButton" instance variable in the .h file
    [button setBackgroundImage:[UIImage imagenamed:@"filename.png"] forControlState:UIControlStateNormal];
}

希望有帮助。

至于为什么你的应用程序在你这样做时崩溃:

view = (UIButton*)subviews;

那是因为“子视图”是所有子视图的 NSArray。您告诉 iOS 将 NSArray * 类型转换为 UIButton *,但它不知道该怎么做。所以你最终导致应用程序崩溃。

在你的 for 循环中,你可能想要做这样的事情(尽管如果你使用我上面的“downloadImage”方法你不应该这样做):

for (view in subviews)
{
    if([view isKindOfClass:[UIButton class]])
    {
        // ------------------------------------------------
        // When you go for(view in subviews), you're saying
        // view = [subviews objectAtIndex:i]
        //
        // Hence view = (UIButton *)subviews become
        // redundant, and not appropriate.
        // ------------------------------------------------

        //view = (UIButton*)subviews; // <--- comment out/delete this line here

        [view setBackgroundImage:[UIImage imageNamed:@"yellow"] forState:UIControlStateNormal];
    }
}
于 2012-10-18T08:07:00.860 回答