我在UILabel
中有一个UITableViewCell
。当用户按下该标签时,我会更改该标签的颜色,并在用户再次按下时将其更改回来。
这适用于所有标签,除非此标签太长并且文本被剪切为“......” - LoremIpsum ......
由于某种原因,3个点不会与文本的其余部分一起改变颜色
任何想法如何让它们与其他文本一起改变? 我这是框架中的一个错误?
// Configure the cell...
if([self.memberships indexOfObject:@(selectedDiscountProgram.uidValue)]!= NSNotFound){
cell.nameLabel.textColor = RabatOrangeColor;
[cell.backgroundImageView setImage:[UIImage rectImageWithGradientFromColor:[UIImage colorWithHex:0xDDDDDD] toColor:[UIImage colorWithHex:0xEEEEEE]]];
[cell.memberButton setImage:[UIImage imageNamed:@"tjek"] forState:UIControlStateNormal];
cell.memberButton.imageEdgeInsets =UIEdgeInsetsMake(0, 0, 0, 9.0f);
//cell.memberButton.frame = CGRectMake(cell.memberButton.frame.origin.x, cell.memberButton.frame.origin.y, 36.0, 36.0);
[cell.memberButton setTitle:[NSString stringWithFormat:@""] forState:UIControlStateNormal];
}else{
cell.nameLabel.textColor = RabatGrayColor;
[cell.backgroundImageView setImage:[UIImage rectImageWithGradientFromColor:[UIImage colorWithHex:0xFFFFFF] toColor:[UIImage colorWithHex:0xF0F0F0]]];
[cell.memberButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[cell.memberButton setTitle:[NSString stringWithFormat:@""] forState:UIControlStateNormal];
[cell.memberButton setTitleColor:RabatGrayColor forState:UIControlStateNormal];
}
答案 0 :(得分:0)
也许你可以在你的自定义单元格中添加一个布尔值来指示像isOrange这样的状态,然后你会有:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
if ([cell isOrange]) {
cell.nameLabel.textColor = RabatGrayColor;
}else{
cell.nameLabel.textColor = RabatOrangeColor;
}
cell.isOrange = !cell.isOrange;
}
您可以在cellForRowAtIndexPath
中添加相同的代码段- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....
if ([cell isOrange]) {
cell.nameLabel.textColor = RabatOrangeColor;
}
else{
cell.nameLabel.textColor = RabatGrayColor;
}
}
答案 1 :(得分:0)
在自定义单元格中添加一个布尔属性,只是为了知道它是否被选中 示例
在您的自定义单元格CustomCell.h
文件
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell
@property (nonatomic, retain) UILabel *nameLabel;
....
....
.... //your other properties for custom cell
@property (nonatomic, assign) BOOL isSelected; //add this boolean variable
@end
CustomCell.m
中的
@synthesize isSelected;
并在初始化时将其设置为
self.isSelected = NO;//because at the beginning no cell is selected
在控制器中,tableview委托方法就像下面这样做
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell == nil)
{
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.nameLabel.text = @"some long text some long text some long text some long text some long text some long text";
//....other settings
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.nameLabel.textColor = [UIColor blackColor]; //initially set to balck color
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath]; //get the cell of the selected index path
if(cell.isSelected)
{
cell.isSelected = NO; //if it is selected set it bool to NO and change color to black
cell.nameLabel.textColor = [UIColor blackColor];
}
else
{
cell.isSelected = YES; //initially it is not selected so select it and set bool to YES and change the colour
cell.nameLabel.textColor = [UIColor orangeColor];
}
}