【问题标题】:Error : reason: '-[UITableViewCellSelectedBackground setImage:] [closed]错误:原因:'-[UITableViewCellSelectedBackground setImage:] [关闭]
【发布时间】:2013-04-08 11:12:35
【问题描述】:

我在我的应用程序中使用 SplitViewController。在主 ViewController 中,我使用的是 Custom table 。

这里是代码: 在 ViewDidload 中:

self.items = [NSMutableArray arrayWithObjects:@"About Company", @"Contact Us", @"Nearest Center",  nil];

[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 100;
    self.tableView.backgroundColor = [UIColor purpleColor];




#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.items count];

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @" " ;
}

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#if USE_CUSTOM_DRAWING
    const NSInteger TOP_LABEL_TAG = 1001;
    const NSInteger BOTTOM_LABEL_TAG = 1002;
    UILabel *topLabel;
    UILabel *bottomLabel;
#endif
    static NSString *CellIdentifier = @"Cell"; // <-- Make sure this matches what you have in the storyboard

    UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
#if USE_CUSTOM_DRAWING
        UIImage *indicatorImage = [UIImage imageNamed:@"indicator.png"];
        cell.accessoryView =  [[UIImageView alloc]initWithImage:indicatorImage];

        const CGFloat LABEL_HEIGHT = 20;
        UIImage *image = [UIImage imageNamed:@"imageA.png"];

        //
        // Create the label for the top row of text
        //
        topLabel =[[UILabel alloc]initWithFrame:CGRectMake(image.size.width + 2.0 * cell.indentationWidth,
                       0.5 * (atableView.rowHeight - 2 * LABEL_HEIGHT),
                     atableView.bounds.size.width -
                     image.size.width - 4.0 * cell.indentationWidth
                     - indicatorImage.size.width,
                     LABEL_HEIGHT)] ;
        [cell.contentView addSubview:topLabel];

        //
        // Configure the properties for the text that are the same on every row
        //
        topLabel.tag = TOP_LABEL_TAG;
        topLabel.backgroundColor = [UIColor clearColor];
        topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
        topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
        topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];

        //
        // Create the label for the top row of text
        //
        bottomLabel =[[UILabel alloc]initWithFrame:CGRectMake(image.size.width + 2.0 * cell.indentationWidth,
                     0.5 * (atableView.rowHeight - 2 * LABEL_HEIGHT) + LABEL_HEIGHT,
                     atableView.bounds.size.width -
                     image.size.width - 4.0 * cell.indentationWidth
                     - indicatorImage.size.width, LABEL_HEIGHT)];
                [cell.contentView addSubview:bottomLabel];

        //
        // Configure the properties for the text that are the same on every row
        //
        bottomLabel.tag = BOTTOM_LABEL_TAG;
        bottomLabel.backgroundColor = [UIColor clearColor];
        bottomLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
        bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
        bottomLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];

        //
        // Create a background image view.
        // 
        cell.backgroundView = [[UIImageView alloc] init];

        cell.selectedBackgroundView =[[UIImageView alloc] init];
      //  cell.selectionStyle = UITableViewCellSelectionStyleNone;
#endif
    }

#if USE_CUSTOM_DRAWING
    else
    {
        topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
        bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
    }

    topLabel.text = [self.items objectAtIndex:indexPath.row];
    bottomLabel.text = [self.items objectAtIndex:indexPath.row];

    //
    // Set the background and selected background images for the text.
    // Since we will round the corners at the top and bottom of sections, we
    // need to conditionally choose the images based on the row index and the
    // number of rows in the section.
    //
    UIImage *rowBackground;
    UIImage *selectionBackground;
    NSInteger sectionRows = [atableView numberOfRowsInSection:[indexPath section]];
    NSInteger row = [indexPath row];
    if (row == 0 && row == sectionRows - 1)
    {
        rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
        selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
    }
    else if (row == 0)
    {
        rowBackground = [UIImage imageNamed:@"topRow.png"];
        selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
    }
    else if (row == sectionRows - 1)
    {
        rowBackground = [UIImage imageNamed:@"bottomRow.png"];
        selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"];
    }
    else
    {
        rowBackground = [UIImage imageNamed:@"middleRow.png"];
        selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"];
    }
    ((UIImageView *)cell.backgroundView).image = rowBackground;
    ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;

    //
    // Here I set an image based on the row. This is just to have something
    // colorful to show on each row.
    //


    switch ([indexPath row]) {
        case 0:
            cell.imageView.image = [UIImage imageNamed:@"imageA.png"];
            break;
        case 1 :
            cell.imageView.image = [UIImage imageNamed:@"imageB.png"];
            break;
        default:
            break;
    }


#else
    //cell.text = [NSString stringWithFormat:@"Cell at row %ld.", [indexPath row]];



    // Configure the cell.
    cell.textLabel.text = [self.items objectAtIndex:indexPath.row]; // Set the MasterViewController's tableView row "Cell" to display selected  array element
   #endif

    return cell;
}

这是我得到的错误:

-[UITableViewCellSelectedBackground setImage:]:无法识别的选择器发送到实例 0x7616300 2013-04-16 11:41:40.841 ICare[1144:c07] * 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因: '-[UITableViewCellSelectedBackground setImage:]: 无法识别的选择器 发送到实例 0x7616300' * 第一次抛出调用栈:(0x1532012 0x1357e7e 0x15bd4bd 0x1521bbc 0x152194e 0xa3da 0x7358fb 0x7359cf 0x71e1bb 0x71c872 0x7275d4 0x72a1c4 0x72a546 0x85ab 0x75c1c7 0x75c232 0x75c4da 0x7738e5 0x7739cb 0x773c76 0x773d71 0x77489b 0x7749b9 0x774a45 0x87a20b 0x6cb2dd 0x136b6b0 0x19ffc0 0x19433c 0x19feaf 0x76a2bd 0x6b2b56 0x6b166f 0x6b1589 0x6b07e4 0x6b061e 0x6b13d9 0x6b42d2 0x75e99c 0x6ab574 0x6ab76f 0x6ab905 0x6b4917 0x67896c 0x67994b 0x68acb5 0x68bbeb 0x67d698 0x2629df9 0x2629ad0 0x14a7bf5 0x14a7962 0x14d8bb6 0x14d7f44 0x14d7e1b 0x67917a 0x67affc 0x7bbd 0x2995 0x1) libc++abi.dylib: 终止调用 抛出异常(lldb)

我错过了一些东西。但我无法解决它,因为我是IOS开发的新手。

应感谢任何帮助或建议。谢谢。

【问题讨论】:

  • 标记一个断点,然后您将到达错误发生的确切行
  • 那么,如果您在理解上仍有问题,请在此处发布有问题的行。看起来您可能在不支持它的类型实例上使用了未定义的选择器(即方法名称)。
  • 另外,正如你的错误所说,setImage: 所以检查你的代码,你在哪里设置图像。自己发现错误将是道德的助推器。然后阅读答案。
  • @giorashc:我只是不知道,需要添加 UIVIEW 。现在我得到了。感谢您的回复
  • @AnoopVaidya:当然,谢谢。

标签: ios xcode ipad uitableview master-detail


【解决方案1】:

那是因为您将图像设置为 UIView 而不是 UIImageView 。 如行中

((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;

您正在尝试将图像设置为 selectedBackgroundView 但它是一个视图,因此您可以将图像视图作为子视图添加到它,或者您可以在背景颜色中使用图案图像。

所以你可以用

复制你的代码

1.

UIImageView *imgView =  [[UIImageView alloc] initWithFrame:cell.selectedBackgroundView.frame];
[imgView setImage:selectionBackground];
[cell.selectedBackgroundView addSubview:imgView];

2.

[cell.selectedBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:selectionBackground]];

它不会给你这个错误并且可以正常工作。 希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2020-04-05
    • 1970-01-01
    相关资源
    最近更新 更多