【发布时间】:2016-02-10 12:49:59
【问题描述】:
嗨,我是 ios 新手,我在我的应用中创建了 一个 UItablaList 没问题
默认情况下,我的 tableList 第一行 textcolor 我已经分配了“RED”
当我单击另一行时,第一行颜色必须为“WHITE”,所选行颜色必须为“RED”,如果我单击默认选择的第一行,则该颜色必须为“RED,请帮帮我,我们如何实现这个逻辑
我的代码:-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"MyCell";
MyCellTableViewCell *cell = (MyCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCellTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
UILabel *lineLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 55, RightMenuTbl.frame.size.width, 1)];
lineLbl.backgroundColor = [UIColor lightGrayColor];
[cell.contentView addSubview:lineLbl];
//remove background for table:-
if (indexPath.row == 0) {
lineLbl.textColor = [UIColor redColor];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
【问题讨论】:
-
最好的做法是,如果你只有一个标签,那么你可以在 UITableViewCell 上使用“detailTextLabel”或 textLabel 属性。像这样。 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.detailTextLabel.text = @"abc"; cell.textLabel.text = @"abc";您可以在 didSelectRowAtIndexPath 中将其颜色设置为 cell.textLabel.textColor = [UIColor redColor];您可以在 deselectRowAtIndexPath cell.textLabel.textColor = [UIColor whiteColor]; 中重置其值
标签: ios objective-c uitableview