Ios 如何将表格的第一个值设置为颜色

Ios 如何将表格的第一个值设置为颜色,ios,iphone,objective-c,uitableview,Ios,Iphone,Objective C,Uitableview,我已经按升序排列了一个数组。我需要显示要着色的表的第一个值。我该怎么做 我的代码是: NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"isDefault" ascending:NO]; sortedArray = [beneficiariesArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]]; NSLog(@"

我已经按升序排列了一个数组。我需要显示要着色的表的第一个值。我该怎么做

我的代码是:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"isDefault"  ascending:NO];
sortedArray = [beneficiariesArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
NSLog(@"This is Sorted Array %@",sortedArray);


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];    
    if (cell == nil)
    {    
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];    
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"Fullname"];
    return cell;
}
您可以使用in
cellforrowatinexpath:
方法进行设置

替换为以下代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];    
    if (cell == nil)
    {    
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];    
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    if(indexPath.row == 0)
     cell.textLabel.textColor = [UIColor redColor]; // set color as you want.

    cell.textLabel.text = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"Fullname"];

    return cell;
}
您可以使用in
cellforrowatinexpath:
方法进行设置

替换为以下代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];    
    if (cell == nil)
    {    
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];    
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    if(indexPath.row == 0)
     cell.textLabel.textColor = [UIColor redColor]; // set color as you want.

    cell.textLabel.text = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"Fullname"];

    return cell;
}

cellforrowatinexpath
方法中。为第一个值设置颜色

-(UITableViewCell *)tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath)indexPath

{

static NSString *simpleTableIdentifier = @"SimpleTableItem";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil)

{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}
if(indexPath.row==0)
{
cell.textLabel.textColor=[UIColor greenColor];
// or you can set background as cell.backgroundColor=[UIColor blackColor];
}
cell.textLabel.text = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"Fullname"]; return cell;
 }

cellforrowatinexpath
方法中。为第一个值设置颜色

-(UITableViewCell *)tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath)indexPath

{

static NSString *simpleTableIdentifier = @"SimpleTableItem";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil)

{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}
if(indexPath.row==0)
{
cell.textLabel.textColor=[UIColor greenColor];
// or you can set background as cell.backgroundColor=[UIColor blackColor];
}
cell.textLabel.text = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"Fullname"]; return cell;
 }
在“行编号”中提供要显示颜色的值,在“任意颜色”中键入可用的颜色名称

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    if (indexPath.section ==0 && indexPath.row == 0) {

        // do your stuff.

        cell.textLabel.textColor = [UIColor greenColor];

    }else{

        // normal stuff

        cell.textLabel.textColor = [UIColor blackColor];
    }


    cell.textLabel.text = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"Fullname"];

    return cell;
}

在“行号”中提供要显示颜色的值,在“任意颜色”中键入可用的颜色名称。

您只需检查单元格索引值,如下所示:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    if (indexPath.section ==0 && indexPath.row == 0) {

        // do your stuff.

        cell.textLabel.textColor = [UIColor greenColor];

    }else{

        // normal stuff

        cell.textLabel.textColor = [UIColor blackColor];
    }


    cell.textLabel.text = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"Fullname"];

    return cell;
}
if (indexPath.row == 0)
{
     // Place your logic
}

您只需检查单元格索引值,如下所示:

if (indexPath.row == 0)
{
     // Place your logic
}
  • 确保已连接表视图委托和数据源
  • 在您的cellForRowAtIndexPath中使用此选项

    if(indexPath.row==0)

    cell.textlab.textColor=[UIColor redColor]

  • 确保已连接表视图委托和数据源
  • 在您的cellForRowAtIndexPath中使用此选项

    if(indexPath.row==0)

    cell.textlab.textColor=[UIColor redColor]


  • 可见单元格有一个属性

    因此,您可以始终设置第一个单元格的颜色

        -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
    
    {
        static NSString *simpleTableIdentifier = @"SimpleTableItem";    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];    
        if (cell == nil)
        {    
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];    
          cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    
        [[tableView.visibleCells objectAtIndex:0] setBackgroundColor:[UIColor redColor]]; //This line changes color for first visible cell
        [[tableView.visibleCells objectAtIndex:1] setBackgroundColor:[UIColor clearColor]]; //This line clears the color of second cell if scrolling in upward direction.
    
        cell.textLabel.text = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"Fullname"];
    
        return cell;
    }
    

    可见单元格有一个属性

    因此,您可以始终设置第一个单元格的颜色

        -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
    
    {
        static NSString *simpleTableIdentifier = @"SimpleTableItem";    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];    
        if (cell == nil)
        {    
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];    
          cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    
        [[tableView.visibleCells objectAtIndex:0] setBackgroundColor:[UIColor redColor]]; //This line changes color for first visible cell
        [[tableView.visibleCells objectAtIndex:1] setBackgroundColor:[UIColor clearColor]]; //This line clears the color of second cell if scrolling in upward direction.
    
        cell.textLabel.text = [[sortedArray objectAtIndex:indexPath.row] objectForKey:@"Fullname"];
    
        return cell;
    }
    

    我这样做了,但是在我scroll@VMC501-哪个值??而且tableView是可滚动的,希望您知道。第一个值消失了。当我第一次不阅读scrollMate时,它看起来很好,这是它的默认行为。有什么解决方案吗?我这样做了,但是在我阅读后,这些值消失了scroll@VMC501-哪个值??而且tableView是可滚动的,希望您知道。第一个值消失了。当我第一次不读scrollMate时,它看起来很好,这是它的默认行为。有什么解决方案吗?