简体   繁体   English

按下时更改表格视图中文本的颜色

[英]Change color of text in tableview when pressed

Alrighty so basically I have a tableView that is filled within the ".plist" with items. 好的,基本上,我有一个tableView ,其中的项目填充在“ .plist”中。

The tableView then gets its functionality from this code: 然后, tableView从以下代码获取其功能:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) { 
//do something
}

if (indexPath.row == 1) { 
//do something
}

However I'd like the item or the text within the tableView to change color when it's pressed and when it's released it should go back to its original color. 但是我希望tableView的项目或文本在按下时以及在释放时更改颜色,应该恢复到原始颜色。

I then added this code to change the background color as I couldn't find anything to just change the text: 然后,我添加了以下代码来更改背景颜色,因为找不到任何内容可以更改文本:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.font=[UIFont fontWithName:@"Helvetica" size:22.0];
    cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];

But then the problem is that it remains with this background color. 但是然后的问题是,它仍然具有这种背景色。

I then tried to create a deselect method like this: 然后,我尝试创建这样的取消选择方法:

-(void)tableView:(UITableView *)tableView deSelectRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.font=[UIFont fontWithName:@"Helvetica" size:22.0];
cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:2.0];

} }

However this does not change the color again. 但是,这不会再次更改颜色。

So basically I either want to the background color or the text color to change when it's being pressed but then change back to the original color when it's released. 因此,基本上,我希望背景颜色或文本颜色在被按下时发生变化,但在释放时又变回原始颜色。 Pretty much like how a UIButton works when it's pressed. 非常类似于按下UIButton时的工作方式。

The correct way to do is set the selectedBackgroundView and backgroundView to the cell. 正确的方法是将selectedBackgroundView和backgroundView设置为该单元格。

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
  if(!cell){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
    selectedBackgroundView.backgrounColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
    cell.selectedBackgroundView = selectedBackgroundView
   }

  return cell;
}

This way you dont have to set the background color when the selection is done and change the color back when deselected. 这样,您不必在完成选择时设置背景色,而在取消选择时无需将背景色更改回去。

If you would like to change the text properties based on selection, sub class UITableViewCell and override setSelected:animated method. 如果要基于选择更改文本属性,请子类UITableViewCell并重写setSelected:animated方法。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
 [super setSelected:selected animated:animated];
 self.textLabel.titleColor.textColor = selected ? selectedColor : notSelectedColor;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM