如何在UITableView中设置UITableViewCell的选定背景颜色

时间:2013-06-05 00:10:38

标签: ios uitableview xib

我不明白为什么这会很重要,但我在使用它时遇到了一些麻烦。

免责声明:这是我的第一个iOS应用程序(或任何Apple开发的问题,应用程序需要在昨天完成,所以请原谅任何匆忙的代码)。

当点击主视图导航控制器上的按钮时,我正在使用SWRevealViewController库创建一个Facebook风格的“侧视图”。它很棒,喜欢它。这个视图只包含两个静态单元格的UITableView,我刚收到一个请求,要求在视图的最底部添加一个单元格。

尝试将第三个单元格“放入”UITableView中似乎并不明显,因此在我的xib中,我刚刚在UITableView外部创建了一个UITableViewCell,并将其放在视图的底部。然后使用标准IBOutlet工作流程将其连接到我的控制器。

回到我的viewDidLoad中,我将流氓单元的外观看起来像其他表格单元,添加了一些措辞,添加了一个处理程序,以便在单击时打开我公司的网站,所有这些都可以正常工作。我为tableView:cellForRowAtIndexPath函数中的“常规”表格单元格做了所有这些操作。

但由于某些原因,当我点击它时,当我点击它时,它不会像我的其他普通单元格一样突出显示!为什么!

CODE:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    } else {
        [[cell.contentView viewWithTag:1] removeFromSuperview];
    }

    UILabel *lblView = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 3.0, 187.0, cell.frame.size.height)];
    [lblView setFont:[UIFont fontWithName:@"FuturaStd-Bold" size:12]];
    lblView.textColor = [UIColor colorWithRed:(255/255.f) green:(241/255.f) blue:(204/255.f) alpha:1.0];
    lblView.tag = 1;
    if (indexPath.row == 0){
        [lblView setText:@"SCHEDULE"];
    } else if (indexPath.row == 1){
        [lblView setText:@"SPEAKERS"];
    }
    lblView.backgroundColor=[UIColor clearColor];

    [cell.contentView addSubview:lblView];

    UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
    av.backgroundColor = [UIColor clearColor];
    av.opaque = NO;
    av.image = [UIImage imageNamed:@"blackBar.png"];
    cell.backgroundView = av;

    UIView *bgColorView = [[UIView alloc] init];
    [bgColorView setBackgroundColor:[UIColor colorWithRed:(240/255.f) green:(145/255.f) blue:(62/255.f) alpha:1.0]];
    [cell setSelectedBackgroundView:bgColorView];

    return cell;
}

以上代码适用于我的“常规单元格”。返回单元格之前的最后一点是当“被点击”时背景突出显示为奇怪的橙色。

下面的代码是相同的但在我的viewDidLoad中,并且在点击时它没有设置背景突出显示颜色。

- (void)viewDidLoad
{
    [super viewDidLoad];

    //... stuff

    UILabel *lblView = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 3.0, 187.0, _roguecell.frame.size.height)];
    [lblView setFont:[UIFont fontWithName:@"FuturaStd-Bold" size:12]];
    lblView.textColor = [UIColor colorWithRed:(255/255.f) green:(241/255.f) blue:(204/255.f) alpha:1.0];
    [lblView setText:@"IT'S A SECRET LOL"];
    lblView.backgroundColor=[UIColor clearColor];

    [_roguecell.contentView addSubview:lblView];

    UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
    av.backgroundColor = [UIColor clearColor];
    av.opaque = NO;
    av.image = [UIImage imageNamed:@"blackBar.png"];
    _roguecell.backgroundView = av;

    UIView *bgColorView = [[UIView alloc] init];
    [bgColorView setBackgroundColor:[UIColor colorWithRed:(240/255.f) green:(145/255.f) blue:(62/255.f) alpha:1.0]];
    [_roguecell setSelectedBackgroundView:bgColorView];

    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [_roguecell addGestureRecognizer:singleFingerTap];
}

也许它与我的xib文件有关我将UITableView向下延伸到整个视图,而流氓UITableViewCell“在它上面”(但显然不在其中)。我试着弄乱桌子和牢房的位置,但这没有做任何事情。

感谢阅读。

1 个答案:

答案 0 :(得分:0)

我最终删除了我的流氓UITableViewCell,并将我的UITableView中的单元格总数从2增加到4.第三个单元格我删除了自定义背景,没有添加任何文本,禁用了用户交互,和使用- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath方法设置高度以填充表格的其余部分,在我的“页脚”单元格底部留下足够的空间。