【问题标题】:How to Set the First Value to Table to Colour如何将第一个值设置为表格颜色
【发布时间】:2014-03-19 06:36:41
【问题描述】:

我已按升序对数组进行了排序。我需要显示要着色的表格的第一个值。我该怎么做呢

我的代码是:

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

【问题讨论】:

    标签: ios iphone objective-c uitableview


    【解决方案1】:
    Here it try it in cellForRowAtIndexPath
    if(indexPath.row == row number)
    {
    cell.backGroundColor = [UIColor anycolor]; // set color as you want.
    }
    

    在行号中提供您希望显示颜色的值,并在任何颜色类型中提供可用的颜色名称。

    【讨论】:

      【解决方案2】:
      -(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】:

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

        【讨论】:

          【解决方案4】:

          有一个可见单元格的属性。

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

              -(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】:
            1. 确保您已连接表视图委托和数据源。
            2. 在你的 cellForRowAtIndexPath 中使用这个。

              如果(indexPath.row==0)

              cell.textLabel.textColor=[UIColor redColor];

            【讨论】:

              【解决方案6】:

              您可以在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;
              }
              

              【讨论】:

              • 我这样做了,但滚动后值消失了
              • @VMC501 - 哪个值?? tableView 也是可滚动的,希望你知道。
              • 第一个值消失了。当我不滚动时它看起来很好
              • 伴侣先读UITableView,这是它的默认行为。
              • 有什么解决办法吗?
              【解决方案7】:

              您只需像这样检查单元格索引值:

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

              【讨论】:

                猜你喜欢
                • 2013-01-27
                • 2021-07-08
                • 1970-01-01
                • 1970-01-01
                • 2012-05-20
                • 2010-11-06
                • 2012-05-21
                • 2012-11-01
                • 1970-01-01
                相关资源
                最近更新 更多