需要有关循环子视图的帮助

时间:2012-10-18 07:26:51

标签: ios

我已经在我的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];
        }
    }
...

但它不起作用

如果我添加此行

  

view =(UIButton *)子视图;

我收到此错误

  

线程1:信号SIGTRAP

如果没有这条线,没有任何事情发生,不知道出了什么问题?

1 个答案:

答案 0 :(得分: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;

这是因为“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];
    }
}