【问题标题】:How can we select tableList row bydefault我们如何默认选择表格列表行
【发布时间】: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


【解决方案1】:

在 .h 文件和 didselectrow 中取 int 作为 selectedRow

selectedRow = indexPath.row;
[yourtableview reloadData];

在单元格中

if (selectedRow== indexPath.row) {
    lineLbl.textColor = set color you want for selected
}else
    lineLbl.textColor = set color you want for not 

选择

【讨论】:

  • 但默认情况下,当我们运行应用程序时,第一行颜色必须是红色
  • ok set selectedRow = 0 in viewwill 出现或出现在您第一次加载表的代码中
  • 嘿@AbhiRam 什么状态
【解决方案2】:

试试这个代码:

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

            NSIndexPath * cellIndexPath = indexPath;

            SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:cellIndexPath];


             cell.nameLbl.textColor = [UIColor redColor];


        }

    (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

    SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:indexPath]; 
cell.nameLbl.textColor = [UIColor whiteColor];

    }

【讨论】:

  • 这是什么,它回答我的问题?
  • 是的,这将改变所选索引标签的文本颜色
  • 我的问题不是请再看一遍
  • 你希望你选择的那一行文本颜色正确地改变
  • 默认情况下,我已经指定 tableList 第一行文本颜色为“RED”,如果我选择其他行,那么第一行(我的意思是这是 allredy 默认选择的行)颜色必须为“WHITE”,如果我选择第一行(我的意思是这是 allredy 默认选择的行)颜色必须像“RED”
【解决方案3】:

试试下面的代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:indexPath];
    cell.nameLbl.textColor = [UIColor redColor];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   SubCategoryCell * cell = (SubCategoryCell*)[tableView cellForRowAtIndexPath:indexPath];
    cell.nameLbl.textColor = [UIColor whiteColor];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 2021-06-14
    • 2012-02-17
    • 1970-01-01
    • 2020-11-11
    • 2012-03-18
    相关资源
    最近更新 更多