0

I have sorted an Array in Ascending order. I need to display the first value of the table to be colored. How can I do this

My code is :

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;
}
iPatel
  • 46,010
  • 16
  • 115
  • 137
iOSDeveloper
  • 461
  • 4
  • 20

7 Answers7

2
Here it try it in cellForRowAtIndexPath
if(indexPath.row == row number)
{
cell.backGroundColor = [UIColor anycolor]; // set color as you want.
}

in row number provide the value where you want the color to be displayed and in anycolor type the colorname available.

Rohit
  • 1,189
  • 12
  • 23
1

At cellForRowAtIndexPath Method. To set color for the first value .

-(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;
 }
Vidhyanand
  • 993
  • 10
  • 21
1
-(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;
}
1
  1. Make sure you have connected your table view delegate and datasource.
  2. In your cellForRowAtIndexPath use this.

    if(indexPath.row==0)

    cell.textLabel.textColor=[UIColor redColor];

Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70
1

There is a property for visible cells.

so you can set the color of the first cell always.

    -(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;
}
Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
0

you can set it by use of indexPath.row in cellForRowAtIndexPath: method.

Replace with following code :

-(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;
}
Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137
0

You need to simply check the cell Index value like this :

if (indexPath.row == 0)
{
     // Place your logic
}
Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70