UITableViewCell textLabel颜色不变

时间:2015-01-20 12:51:13

标签: ios uitableview

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell       =   (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];

    if (cell == nil)
    {
        cell                    =   [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.frame              =   CGRectZero;
        cell.backgroundColor    =   [UIColor whiteColor];
        cell.selectionStyle     =   UITableViewCellSelectionStyleNone;
        cell.accessoryType      =   UITableViewCellAccessoryNone;




        UIImageView* iconView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10 , 30, 30)];
        iconView.layer.cornerRadius = 15;
        iconView.image  =   [iconArray objectAtIndex:indexPath.row];
        [cell.contentView addSubview:iconView];

        UILabel* textLabel                 =    [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 250, 50)];
        textLabel.font                      =   [UIFont lightFontWithSize:18];
        textLabel.textAlignment             =   NSTextAlignmentLeft;
        textLabel.textColor                 =   [UIColor blackColor];
        textLabel.numberOfLines             =   2;
        textLabel.tag                       =   1;
        cell.textLabel.highlightedTextColor = [UIColor redColor];
        textLabel.text                      =   [menuArray objectAtIndex:indexPath.row];
        [cell.contentView addSubview:textLabel];

    }
    return cell;
}

2 个答案:

答案 0 :(得分:1)

正在工作

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell       =   (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];

    if (cell == nil)
    {
      cell                    =   [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
      cell.selectionStyle     =   UITableViewCellSelectionStyleNone;
      cell.accessoryType      =   UITableViewCellAccessoryNone;

      UIImageView* iconView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10 , 30, 30)];
      iconView.layer.cornerRadius = 15;
      iconView.image  =   [iconArray objectAtIndex:indexPath.row];
      [cell.contentView addSubview:iconView];

      UILabel* textLabel                 =    [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 250, 50)];
      textLabel.font                      =   [UIFont lightFontWithSize:18];
      textLabel.textAlignment             =   NSTextAlignmentLeft;
      textLabel.textColor                 =   [UIColor blackColor];
      textLabel.numberOfLines             =   2;
      textLabel.tag                       =   1;
      textLabel.text                      =   [menuArray objectAtIndex:indexPath.row];
      [cell.contentView addSubview:textLabel];

   }
   return cell;
}

答案 1 :(得分:1)

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell       =   (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];

    if (cell == nil)
    {
        cell                    =   [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
//        cell.textLabel.textColor = [UIColor whiteColor];
//        cell.frame              =   CGRectZero;
        cell.backgroundColor    =   [UIColor whiteColor];
        cell.selectionStyle     =   UITableViewCellSelectionStyleNone;
        cell.accessoryType      =   UITableViewCellAccessoryNone;

        UIImageView* iconView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10 , 30, 30)];
        iconView.layer.cornerRadius = 15;
        iconView.tag                       =   100;

        [cell.contentView addSubview:iconView];

        UILabel* txtLbl                 =    [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 250, 50)];
        txtLbl.font                      =   [UIFont lightFontWithSize:18];
        txtLbl.textAlignment             =   NSTextAlignmentLeft;
        txtLbl.textColor                 =   [UIColor blackColor];
        txtLbl.numberOfLines             =   2;
        txtLbl.tag                       =   101;
        txtLbl.highlightedTextColor = [UIColor redColor];
        [cell.contentView addSubview:txtLbl];
    }

    UIImageView* iconView = (UIImageView *)[cell.contentView viewWithTag:100];
    iconView.image  =   [iconArray objectAtIndex:indexPath.row];


    UILabel* txtLbl                     = (UILabel *) [cell.contentView viewWithTag:101];
    txtLbl.text                      =   [menuArray objectAtIndex:indexPath.row];

    return cell;
}

不要将标签命名为textLabel它默认使用..并且不要在(cell == nil)内设置子视图的值因为tableview重用了单元格。始终(cell == nil)阻止未调用..