0

我在我的应用程序中使用 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' *First throw call stack: (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: terminate called throwing an exception (lldb)

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

任何帮助或建议应不胜感激。谢谢你。

4

1 回答 1

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]];

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

于 2013-04-16T06:59:01.763 回答