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

时间:2014-03-19 06:36:42

标签: ios iphone objective-c uitableview

我按升序排序了一个数组。我需要显示要着色的表的第一个值。我怎么能这样做

我的代码是:

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;
}

7 个答案:

答案 0 :(得分:2)

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

提供要显示颜色的值,并在任何颜色类型中提供可用的颜色名称。

答案 1 :(得分:1)

cellForRowAtIndexPath方法。设置第一个值的颜色。

-(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;
 }

答案 2 :(得分: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;
}

答案 3 :(得分:1)

  1. 确保已连接表视图委托和数据源。
  2. 在你的cellForRowAtIndexPath中使用它。

    如果(indexPath.row == 0)

    cell.textLabel.textColor = [UIColor redColor];

答案 4 :(得分: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;
    }

    [[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;
}

答案 5 :(得分:0)

您可以在cellForRowAtIndexPath:方法中使用indexPath.row进行设置。

替换为以下代码:

-(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;
}

答案 6 :(得分:0)

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

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