【问题标题】:Custom label background gets covered when cell is selected选择单元格时,自定义标签背景被覆盖
【发布时间】:2011-11-10 13:48:49
【问题描述】:

当我的单元格突出显示时,我遇到了一个奇怪的问题。

只是为了更好的给大家介绍一下问题是什么,看看这两张图:

这是未选中时单元格的外观(正常状态):http://cl.ly/0n193u3U1o403x1s0m3z

这是单元格突出显示时的外观(在点击期间):http://cl.ly/1o2U400D3L0b3n3m1N1J

如您所见,当单元格被选中时,第二个标签的背景似乎被忽略了。 我不希望这种情况发生。我只想让第二个标签保持原样。

这就是我创建单元格的方式(有几种类型,每种都使用不同的单元格标识符)。

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                   reuseIdentifier:CellTableIdentifier] autorelease];
    cell.backgroundView = [[[UIView alloc] init] autorelease];
}

switch (indexPath.section) {
    ...
    ...
    case kTableSectionPending:


        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.tableView.frame.size.width - 20, 30.0)] autorelease];
        UILabel *label2 = [[[UILabel alloc] initWithFrame:CGRectMake(10, 50, self.tableView.frame.size.width - 20, 30.0)] autorelease];
        label.text = NSLocalizedString(@"IncompletePath", @"");
        label.font = [UIFont boldSystemFontOfSize:16];
        label.textAlignment = UITextAlignmentCenter;
        label.backgroundColor = [UIColor clearColor];
        label.layer.cornerRadius = 10;
        FFRoute *lastPoint = ((FFRoute *) [[[FFSQLite sharedSingleton] getRoutesFromItinerary:itinerary] lastObject]);
        label2.text = [NSString stringWithFormat:@"%@: %@", NSLocalizedString(@"LastPoint", @""), [self getStringFromTimestampOfFFRoute:lastPoint]];
        label2.font = [UIFont systemFontOfSize:15];
        label2.textAlignment = UITextAlignmentCenter;
        label2.textColor = [UIColor whiteColor];
        label2.backgroundColor = [UIColor colorWithRed:68/255.f green:82/255.f blue:124/255.f alpha:1];
        label2.layer.cornerRadius = 10;
        [label2 setOpaque:YES];
        [cell.contentView addSubview:label];
        [cell.contentView addSubview:label2];

        // Set up background color
        UIColor *bgcolor = [UIColor colorWithRed:250/255.f green:212/255.f blue:137/255.f alpha:1];
        cell.backgroundView.backgroundColor = bgcolor;            
        break;
}

我尝试将 Opaque 属性设置为 true,但没有成功。

我错过了什么?谢谢

【问题讨论】:

    标签: iphone uitableview background label


    【解决方案1】:

    创建单元格时,请执行以下操作:

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    可以突出显示、选择单元格或两者兼而有之。您可能想要自定义您的绘图,使其在选定状态下绘制(但仅在突出显示状态下正常绘制)。

    【讨论】:

    • 你的意思是我应该创建一个完全自定义的单元格,对吧?因为目前我只是在一个空的标签上添加标签。
    • 嗯,这取决于。但是我不久前通过一个小型测试项目发现,在 UITableViews 中突出显示和选择的行为不是很好。我发现自己做画最容易(即使那样也没有得到我想要的确切行为,但现在不记得细节了)。您仍然可以使用标签和其他视图来构建单元格,但单元格“基础”(UITableViewCell)应该使用自定义绘图。
    【解决方案2】:

    在 iOS 6 中,您可以使用 UITableViewDelegate 中的两种方法来自定义单元格及其子视图的突出显示:

    - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell setBackgroundColor:[UIColor lightGrayColor]];  // Your highlight color
        // Make changes to subviews in cell
    }
    
    - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [UIView animateWithDuration:0.5f animations:^{
            [cell setBackgroundColor:[UIColor whiteColor]];  // Your unhighlight
            // Revert back changes for subviews
        }];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 2022-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多