Ios 如何在不丢失文本的情况下更改选定单元格的背景色
我想更改选定单元格的背景色,并保留该单元格上的文本。然后我这样做了,它可以改变单元格的颜色,但单元格上的文本将消失Ios 如何在不丢失文本的情况下更改选定单元格的背景色,ios,uitableview,colors,background,Ios,Uitableview,Colors,Background,我想更改选定单元格的背景色,并保留该单元格上的文本。然后我这样做了,它可以改变单元格的颜色,但单元格上的文本将消失 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"MessageCell"; UITableViewCell *cell = [tabl
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MessageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.receivedData objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"MessageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UIView *bgColorView = [[UIView alloc]init];
bgColorView.backgroundColor = [UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:0.5];
cell.selectedBackgroundView = bgColorView;
}
您正在使用UIView的新实例(其中没有文本,但有新颜色)替换当前视图(其中包含文本的视图)。这就是你的文字消失的原因。您应该更改selectedBackgroundview的背景色属性,如下所示:
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:0.5];
尽管我认为您甚至可以放弃
selectedBackgroundView
部分,只需在didSelectRowAtIndexPath
中执行cell.backgroundColor=…
,文本是什么颜色?可能重复的文本不应在新单元格中排队。使用表视图方法CellForRowatineXpath:获取对所选单元格的引用。