我不明白为什么这会起作用,但我在使其工作时遇到了一些麻烦。免责声明:这是我的第一个iOS应用程序(或任何Apple开发的应用程序,应用程序需要在昨天完成,所以请原谅任何看起来很匆忙的代码)。如何在UITableView中设置UITableViewCell的选定背景颜色
我使用SWRevealViewController库来创建一个Facebook的esque“侧视图”时,主视图导航控制器上的按钮被点击。它很棒,喜欢它。这个视图拥有一个只有两个静态单元的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“顶部”它(而不是在它很明显)。我试着弄乱桌子和牢房的位置,但那没有做任何事。
感谢您的阅读。
因此,您将流氓单元添加到表视图标题? –
@verbumdei不,我的厦门国际银行视图层次结构如下所示: -UIView --UIToolbar --UILabel --UITableView --UITableViewCell – trainface
如果你的表使用静态的细胞,你甚至不应该执行表视图数据源方法。您应该将IBOutlet添加到单元格或UI元素本身,并将它们填充到viewDiLoad中,就像您使用其他任何标签或图像视图一样。 – rdelmar