我在UILabel
中有一个UITableViewCell
。在最后一行选择中,我需要更改所有行的UILabel的文本颜色。我怎么能这样做?
答案 0 :(得分:1)
BOOL *selectlastindex;
- (void)viewDidLoad {
[super viewDidLoad];
selectlastindex = NO;
// suggestionList is my NSArray (Data source)
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (selectlastindex == YES) {
if (indexPath.row == suggestionList.count - 1) {
suggestiontitle.textColor = [UIColor blackColor];
}else{
suggestiontitle.textColor = [UIColor grayColor];
}
}else{
suggestiontitle.textColor = [UIColor blackColor];
}
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"click");
if (indexPath.row == suggestionList.count - 1) {
selectlastindex = YES;
[self.Tableview reloadData];
}else{
selectlastindex = NO;
[self.Tableview reloadData];
}
}
输出:CHECK OUTPUT
答案 1 :(得分:0)
请尝试在willDisplayCell:
中调用您的着色代码:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
// do your logic here:
if (selectlastindex == YES) {
if (indexPath.row == suggestionList.count - 1) {
suggestiontitle.textColor = [UIColor blackColor];
}else{
suggestiontitle.textColor = [UIColor grayColor];
}
}else{
suggestiontitle.textColor = [UIColor blackColor];
}
}
答案 2 :(得分:-1)
您可以设置类似isLastRowSelected
的布尔值,如果用户选择最后一行然后重新加载tableView,则将其设置为true。
在cellForRowAtIndexPath:
回调中,应用条件
if isLastRowSelected {
cell.label.textColor = UIColor.desiredColor
}
else {
cell.label.textColor = UIColor.defaultColor
}
希望它会有所帮助:)
完整说明:
在您的班级中设置存储的属性
var isLastRowSelected = false
然后
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// First add the selectedItem in your selectedItems and in the last add this
isLastRowSelected = selectedItems.count == datasource.count
tableView.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if isLastRowSelected {
cell.label.textColor = UIColor.desiredColor
}
else {
cell.label.textColor = UIColor.defaultColor
}
}