【问题标题】:How to customize the background/border colors of a grouped table view cell?如何自定义分组表格视图单元格的背景/边框颜色?
【发布时间】:2010-09-28 21:28:49
【问题描述】:

我想自定义分组样式 UITableView 的背景和边框颜色。

我可以使用以下方法自定义背景颜色:

tableView.contentView.backgroundColor = [UIColor greenColor];

但是边框颜色还是不知道怎么改。

如何自定义分组样式表视图的这两个方面?

【问题讨论】:

  • 确保您的 UITableViewController 的 IBOutlet 视图已设置很重要,否则透明度将不起作用!
  • 不确定你的代码行是如何工作的。 tableView 似乎没有 contentView 属性。
  • 线程是关于 UITableViewCell 的背景 而不是关于 UITableView(就像问题所暗示的那样)。真正的答案是@dizy 的答案。

标签: ios objective-c iphone uitableview cocoa-touch


【解决方案1】:

更新:在 iPhone OS 3.0 和更高版本中,UITableViewCell 现在有一个 backgroundColor 属性,这使得这非常容易(尤其是与 [UIColor colorWithPatternImage:] 初始化程序结合使用)。但我会将 2.0 版本的答案留给任何需要它的人……


这比实际应该的要难。当我不得不这样做时,我是这样做的:

您需要将 UITableViewCell 的 backgroundView 属性设置为自定义 UIView,该 UIView 以适当的颜色绘制边框和背景本身。此视图需要能够以 4 种不同的模式绘制边框,部分中的第一个单元格的顶部圆角,部分中的最后一个单元格的底部圆角,部分中间的单元格没有圆角, 对于包含一个单元格的部分,所有 4 个角都被舍入。

不幸的是,我不知道如何自动设置此模式,所以我不得不在 UITableViewDataSource 的 -cellForRowAtIndexPath 方法中设置它。

这是一个真正的 PITA,但我已经与 Apple 工程师确认这是目前唯一的方法。

更新这是自定义背景视图的代码。有一个绘图错误使圆角看起来有点滑稽,但我们改用了不同的设计,并在我有机会修复它之前取消了自定义背景。不过这可能对你很有帮助:

//
//  CustomCellBackgroundView.h
//
//  Created by Mike Akers on 11/21/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef enum  {
    CustomCellBackgroundViewPositionTop, 
    CustomCellBackgroundViewPositionMiddle, 
    CustomCellBackgroundViewPositionBottom,
    CustomCellBackgroundViewPositionSingle
} CustomCellBackgroundViewPosition;

@interface CustomCellBackgroundView : UIView {
    UIColor *borderColor;
    UIColor *fillColor;
    CustomCellBackgroundViewPosition position;
}

    @property(nonatomic, retain) UIColor *borderColor, *fillColor;
    @property(nonatomic) CustomCellBackgroundViewPosition position;
@end

//
//  CustomCellBackgroundView.m
//
//  Created by Mike Akers on 11/21/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "CustomCellBackgroundView.h"

static void addRoundedRectToPath(CGContextRef context, CGRect rect,
                                 float ovalWidth,float ovalHeight);

@implementation CustomCellBackgroundView
@synthesize borderColor, fillColor, position;

- (BOOL) isOpaque {
    return NO;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(c, [fillColor CGColor]);
    CGContextSetStrokeColorWithColor(c, [borderColor CGColor]);

    if (position == CustomCellBackgroundViewPositionTop) {
        CGContextFillRect(c, CGRectMake(0.0f, rect.size.height - 10.0f, rect.size.width, 10.0f));
        CGContextBeginPath(c);
        CGContextMoveToPoint(c, 0.0f, rect.size.height - 10.0f);
        CGContextAddLineToPoint(c, 0.0f, rect.size.height);
        CGContextAddLineToPoint(c, rect.size.width, rect.size.height);
        CGContextAddLineToPoint(c, rect.size.width, rect.size.height - 10.0f);
        CGContextStrokePath(c);
        CGContextClipToRect(c, CGRectMake(0.0f, 0.0f, rect.size.width, rect.size.height - 10.0f));
    } else if (position == CustomCellBackgroundViewPositionBottom) {
        CGContextFillRect(c, CGRectMake(0.0f, 0.0f, rect.size.width, 10.0f));
        CGContextBeginPath(c);
        CGContextMoveToPoint(c, 0.0f, 10.0f);
        CGContextAddLineToPoint(c, 0.0f, 0.0f);
        CGContextStrokePath(c);
        CGContextBeginPath(c);
        CGContextMoveToPoint(c, rect.size.width, 0.0f);
        CGContextAddLineToPoint(c, rect.size.width, 10.0f);
        CGContextStrokePath(c);
        CGContextClipToRect(c, CGRectMake(0.0f, 10.0f, rect.size.width, rect.size.height));
    } else if (position == CustomCellBackgroundViewPositionMiddle) {
        CGContextFillRect(c, rect);
        CGContextBeginPath(c);
        CGContextMoveToPoint(c, 0.0f, 0.0f);
        CGContextAddLineToPoint(c, 0.0f, rect.size.height);
        CGContextAddLineToPoint(c, rect.size.width, rect.size.height);
        CGContextAddLineToPoint(c, rect.size.width, 0.0f);
        CGContextStrokePath(c);
        return; // no need to bother drawing rounded corners, so we return
    }

    // At this point the clip rect is set to only draw the appropriate
    // corners, so we fill and stroke a rounded rect taking the entire rect

    CGContextBeginPath(c);
    addRoundedRectToPath(c, rect, 10.0f, 10.0f);
    CGContextFillPath(c);  

    CGContextSetLineWidth(c, 1);  
    CGContextBeginPath(c);
    addRoundedRectToPath(c, rect, 10.0f, 10.0f);  
    CGContextStrokePath(c); 
}


- (void)dealloc {
    [borderColor release];
    [fillColor release];
    [super dealloc];
}


@end

static void addRoundedRectToPath(CGContextRef context, CGRect rect,
                                float ovalWidth,float ovalHeight)

{
    float fw, fh;

    if (ovalWidth == 0 || ovalHeight == 0) {// 1
        CGContextAddRect(context, rect);
        return;
    }

    CGContextSaveGState(context);// 2

    CGContextTranslateCTM (context, CGRectGetMinX(rect),// 3
                           CGRectGetMinY(rect));
    CGContextScaleCTM (context, ovalWidth, ovalHeight);// 4
    fw = CGRectGetWidth (rect) / ovalWidth;// 5
    fh = CGRectGetHeight (rect) / ovalHeight;// 6

    CGContextMoveToPoint(context, fw, fh/2); // 7
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);// 8
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);// 9
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);// 10
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // 11
    CGContextClosePath(context);// 12

    CGContextRestoreGState(context);// 13
}

【讨论】:

  • 您能帮我使用您的代码吗? => stackoverflow.com/questions/7309580/…
  • 为什么不使用seperatorColor?对我来说,它为单元格和边框之间的分隔线着色。
  • 我没有使用drawRect CG弧形路径,而是使用了所有角都被圆角的剪辑视图。对于非第一行,我给它一个负 Y,对于非最后一行,我让它超高。
【解决方案2】:

我一直遇到这个问题并尝试了很多组合,因为我注意到对于某些单元它工作正常,但对于其他单元则不行。

奇怪的是,我发现可以将 cell.backgroundColor 设置为 lightGrayColor 并且一切正常 - 但 blueColor 导致我无法更新外部边缘。

除非使用绿色真的很重要 - 也许您可能想尝试一下。这可能是一个让人们在指示单元格被选中时只使用灰色的功能。

【讨论】:

    【解决方案3】:

    首先感谢这段代码。我在这个函数中做了一些绘图更改,以消除绘图的角落问题。

    -(void)drawRect:(CGRect)rect 
    {
        // Drawing code
    
        CGContextRef c = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(c, [fillColor CGColor]);
        CGContextSetStrokeColorWithColor(c, [borderColor CGColor]);
        CGContextSetLineWidth(c, 2);
    
        if (position == CustomCellBackgroundViewPositionTop) {
    
            CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
            CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
            minx = minx + 1;
            miny = miny + 1;
    
            maxx = maxx - 1;
            maxy = maxy ;
    
            CGContextMoveToPoint(c, minx, maxy);
            CGContextAddArcToPoint(c, minx, miny, midx, miny, ROUND_SIZE);
            CGContextAddArcToPoint(c, maxx, miny, maxx, maxy, ROUND_SIZE);
            CGContextAddLineToPoint(c, maxx, maxy);
    
            // Close the path
            CGContextClosePath(c);
            // Fill & stroke the path
            CGContextDrawPath(c, kCGPathFillStroke);        
            return;
        } else if (position == CustomCellBackgroundViewPositionBottom) {
    
            CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
            CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
            minx = minx + 1;
            miny = miny ;
    
            maxx = maxx - 1;
            maxy = maxy - 1;
    
            CGContextMoveToPoint(c, minx, miny);
            CGContextAddArcToPoint(c, minx, maxy, midx, maxy, ROUND_SIZE);
            CGContextAddArcToPoint(c, maxx, maxy, maxx, miny, ROUND_SIZE);
            CGContextAddLineToPoint(c, maxx, miny);
            // Close the path
            CGContextClosePath(c);
            // Fill & stroke the path
            CGContextDrawPath(c, kCGPathFillStroke);    
            return;
        } else if (position == CustomCellBackgroundViewPositionMiddle) {
            CGFloat minx = CGRectGetMinX(rect) , maxx = CGRectGetMaxX(rect) ;
            CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
            minx = minx + 1;
            miny = miny ;
    
            maxx = maxx - 1;
            maxy = maxy ;
    
            CGContextMoveToPoint(c, minx, miny);
            CGContextAddLineToPoint(c, maxx, miny);
            CGContextAddLineToPoint(c, maxx, maxy);
            CGContextAddLineToPoint(c, minx, maxy);
    
            CGContextClosePath(c);
            // Fill & stroke the path
            CGContextDrawPath(c, kCGPathFillStroke);    
            return;
        }
    }
    

    【讨论】:

    • 您的代码的线宽约为 2 px。当我尝试将 CGContextSetLineWidth 设置为 1 时,它仍然很厚。这是为什么呢?
    • 我也有同样的问题?为什么它比系统厚。
    【解决方案4】:

    感谢您提供的代码,这正是我想要的。我还在 Vimal 的代码中添加了以下代码,以实现 CustomCellBackgroundViewPositionSingle 单元格的情况。 (所有四个角都是圆角。)

    
    else if (position == CustomCellBackgroundViewPositionSingle)
    {
            CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
            CGFloat miny = CGRectGetMinY(rect) , midy = CGRectGetMidY(rect) , maxy = CGRectGetMaxY(rect) ;
            minx = minx + 1;
            miny = miny + 1;
    
            maxx = maxx - 1;
            maxy = maxy - 1;
    
            CGContextMoveToPoint(c, minx, midy);
            CGContextAddArcToPoint(c, minx, miny, midx, miny, ROUND_SIZE);
            CGContextAddArcToPoint(c, maxx, miny, maxx, midy, ROUND_SIZE);
            CGContextAddArcToPoint(c, maxx, maxy, midx, maxy, ROUND_SIZE);
            CGContextAddArcToPoint(c, minx, maxy, minx, midy, ROUND_SIZE);
    
            // Close the path
            CGContextClosePath(c);
            // Fill & stroke the path
            CGContextDrawPath(c, kCGPathFillStroke);                
            return;     
    }
    

    【讨论】:

      【解决方案5】:

      我在 Mike Akers 的上述 CustomCellBackgroundView 代码中遇到了一件可能对其他人有用的事情:

      cell.backgroundView 在重复使用单元格时不会自动重绘,并且更改 backgroundView 的位置变量不会影响重复使用的单元格。这意味着长桌在给定位置时将错误地绘制cell.backgroundViews

      要解决此问题而不必在每次显示一行时都创建一个新的 backgroundView,请在 -[UITableViewController tableView:cellForRowAtIndexPath:] 的末尾调用 [cell.backgroundView setNeedsDisplay]。或者对于更可重用的解决方案,覆盖 CustomCellBackgroundView 的位置设置器以包含 [self setNeedsDisplay]

      【讨论】:

      • 关于覆盖 -setPosition 的好主意
      【解决方案6】:

      非常感谢所有发布代码的人。这非常有用。

      我得出了一个类似的解决方案来更改分组表视图单元格的突出显示颜色。基本上是 UITableViewCell 的 selectedBackgroundView(不是 backgroundView)。据我所知,即使在 iPhone OS 3.0 上仍然需要这个 PITA 解决方案......

      下面的代码对使用渐变而不是一种纯色渲染高光进行了更改。边框渲染也被删除。享受吧。

      //
      //  CSCustomCellBackgroundView.h
      //
      
      #import <UIKit/UIKit.h>
      
      typedef enum  
      {
          CustomCellBackgroundViewPositionTop, 
          CustomCellBackgroundViewPositionMiddle, 
          CustomCellBackgroundViewPositionBottom,
          CustomCellBackgroundViewPositionSingle,
          CustomCellBackgroundViewPositionPlain
      } CustomCellBackgroundViewPosition;
      
      @interface CSCustomCellBackgroundView : UIView 
      {
          CustomCellBackgroundViewPosition position;
       CGGradientRef gradient;
      }
      
      @property(nonatomic) CustomCellBackgroundViewPosition position;
      
      @end
      
      
      
      //
      //  CSCustomCellBackgroundView.m
      //
      
      #import "CSCustomCellBackgroundView.h"
      
      
      
      #define ROUND_SIZE 10
      
      
      static void addRoundedRectToPath(CGContextRef context, CGRect rect,
               float ovalWidth,float ovalHeight);
      
      
      @implementation CSCustomCellBackgroundView
      
      
      @synthesize position;
      
      - (BOOL) isOpaque 
      {
          return NO;
      }
      
      - (id)initWithFrame:(CGRect)frame 
      {
          if (self = [super initWithFrame:frame]) 
       {
              // Initialization code
        const float* topCol = CGColorGetComponents([[UIColor redColor] CGColor]);
        const float* bottomCol = CGColorGetComponents([[UIColor blueColor] CGColor]);
      
        CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
        /*
        CGFloat colors[] =
        {
         5.0 / 255.0, 140.0 / 255.0, 245.0 / 255.0, 1.00,
         1.0 / 255.0,  93.0 / 255.0, 230.0 / 255.0, 1.00,
        };*/
        CGFloat colors[]=
        {
         topCol[0], topCol[1], topCol[2], topCol[3],
         bottomCol[0], bottomCol[1], bottomCol[2], bottomCol[3]
        };
        gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
        CGColorSpaceRelease(rgb);
          }
          return self;
      }
      
      
      -(void)drawRect:(CGRect)rect 
      {
          // Drawing code
      
          CGContextRef c = UIGraphicsGetCurrentContext();
      
          if (position == CustomCellBackgroundViewPositionTop) 
       {
      
              CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
              CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
              minx = minx + 1;
              miny = miny + 1;
      
              maxx = maxx - 1;
              maxy = maxy ;
      
              CGContextMoveToPoint(c, minx, maxy);
              CGContextAddArcToPoint(c, minx, miny, midx, miny, ROUND_SIZE);
              CGContextAddArcToPoint(c, maxx, miny, maxx, maxy, ROUND_SIZE);
              CGContextAddLineToPoint(c, maxx, maxy);
      
              // Close the path
              CGContextClosePath(c);
      
        CGContextSaveGState(c);
        CGContextClip(c);
        CGContextDrawLinearGradient(c, gradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
        CGContextRestoreGState(c);
      
              return;
          } 
       else if (position == CustomCellBackgroundViewPositionBottom) 
       {
      
              CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
              CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
              minx = minx + 1;
              miny = miny + 1;
      
              maxx = maxx - 1;
              maxy = maxy - 1;
      
              CGContextMoveToPoint(c, minx, miny);
              CGContextAddArcToPoint(c, minx, maxy, midx, maxy, ROUND_SIZE);
              CGContextAddArcToPoint(c, maxx, maxy, maxx, miny, ROUND_SIZE);
              CGContextAddLineToPoint(c, maxx, miny);
              // Close the path
              CGContextClosePath(c);
      
        CGContextSaveGState(c);
        CGContextClip(c);
        CGContextDrawLinearGradient(c, gradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
        CGContextRestoreGState(c);
      
              return;
          } 
       else if (position == CustomCellBackgroundViewPositionMiddle) 
       {
              CGFloat minx = CGRectGetMinX(rect) , maxx = CGRectGetMaxX(rect) ;
              CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
              minx = minx + 1;
              miny = miny + 1;
      
              maxx = maxx - 1;
              maxy = maxy ;
      
              CGContextMoveToPoint(c, minx, miny);
              CGContextAddLineToPoint(c, maxx, miny);
              CGContextAddLineToPoint(c, maxx, maxy);
              CGContextAddLineToPoint(c, minx, maxy);
        // Close the path
              CGContextClosePath(c);
      
        CGContextSaveGState(c);
        CGContextClip(c);
        CGContextDrawLinearGradient(c, gradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
        CGContextRestoreGState(c);
      
              return;
          }
       else if (position == CustomCellBackgroundViewPositionSingle)
       {
              CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
              CGFloat miny = CGRectGetMinY(rect) , midy = CGRectGetMidY(rect) , maxy = CGRectGetMaxY(rect) ;
              minx = minx + 1;
              miny = miny + 1;
      
              maxx = maxx - 1;
              maxy = maxy - 1;
      
              CGContextMoveToPoint(c, minx, midy);
              CGContextAddArcToPoint(c, minx, miny, midx, miny, ROUND_SIZE);
              CGContextAddArcToPoint(c, maxx, miny, maxx, midy, ROUND_SIZE);
              CGContextAddArcToPoint(c, maxx, maxy, midx, maxy, ROUND_SIZE);
              CGContextAddArcToPoint(c, minx, maxy, minx, midy, ROUND_SIZE);
              // Close the path
              CGContextClosePath(c);              
      
        CGContextSaveGState(c);
        CGContextClip(c);
        CGContextDrawLinearGradient(c, gradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
        CGContextRestoreGState(c);
      
              return;         
       } 
       else if (position == CustomCellBackgroundViewPositionPlain) {
          CGFloat minx = CGRectGetMinX(rect);
          CGFloat miny = CGRectGetMinY(rect), maxy = CGRectGetMaxY(rect) ;
          CGContextDrawLinearGradient(c, gradient, CGPointMake(minx,miny), CGPointMake(minx,maxy), kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
          return;
      }
      
      }
      
      - (void)dealloc 
      {
          CGGradientRelease(gradient);
          [super dealloc];
      }
      
      
      - (void) setPosition:(CustomCellBackgroundViewPosition)inPosition
      {
       if(position != inPosition)
       {
        position = inPosition;
        [self setNeedsDisplay];
       }
      }
      
      @end
      
      
      static void addRoundedRectToPath(CGContextRef context, CGRect rect,
               float ovalWidth,float ovalHeight)
      
      {
          float fw, fh;
      
          if (ovalWidth == 0 || ovalHeight == 0) {// 1
              CGContextAddRect(context, rect);
              return;
          }
      
          CGContextSaveGState(context);// 2
      
          CGContextTranslateCTM (context, CGRectGetMinX(rect),// 3
               CGRectGetMinY(rect));
          CGContextScaleCTM (context, ovalWidth, ovalHeight);// 4
          fw = CGRectGetWidth (rect) / ovalWidth;// 5
          fh = CGRectGetHeight (rect) / ovalHeight;// 6
      
          CGContextMoveToPoint(context, fw, fh/2); // 7
          CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);// 8
          CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);// 9
          CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);// 10
          CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // 11
          CGContextClosePath(context);// 12
      
          CGContextRestoreGState(context);// 13
      }
      

      【讨论】:

      • 这真的很棒,也是我想要的。我需要两个选择。将 bg 设置为渐变色或纯色。我只是通过将渐变颜色设置为我想要的相同纯色来设置纯色。它虽然强加了不必要的计算。如果有这样的选择就好了。
      【解决方案7】:

      感谢这篇超级有用的帖子。万一有人(比如我!)想要一个完全空的单元格背景,而不是通过 IB 中的图像/文本/其他内容自定义它,并且无法弄清楚如何摆脱愚蠢的边框/填充/即使您在 IB 中将其设置为清除背景...这是我用来解决问题的代码!

      
      - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {
      
          static NSString *cellId = @"cellId";
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellId];
          if (cell == nil) {
              [[NSBundle mainBundle] loadNibNamed:@"EditTableViewCell" owner:self options:nil];
              cell = cellIBOutlet;
              self.cellIBOutlet = nil;
          }
      
          cell.backgroundView = [[[UIView alloc] initWithFrame: CGRectZero] autorelease];
          [cell.backgroundView setNeedsDisplay];
      
          ... any other cell customizations ...
      
          return cell;
      }
      

      希望这会对其他人有所帮助!似乎很有魅力。

      【讨论】:

      • 您的解决方案存在内存泄漏。您需要自动释放您设置为cell.backgroundView 的视图。
      【解决方案8】:

      可以通过设置自定义边框颜色

      tableView.separatorColor
      

      【讨论】:

        【解决方案9】:

        我知道答案与更改分组表格单元格有关,但如果有人想更改表格视图的背景颜色:

        不仅需要设置:

        tableview.backgroundColor = color;
        

        你还需要改变或去掉背景视图:

        tableview.backgroundView = nil;  
        

        【讨论】:

        • 嘿,谢谢。您专门针对这个问题给出了正确答案。
        【解决方案10】:

        改变表格视图边框颜色:

        在.h:

        #import <QuartzCore/QuartzCore.h>
        

        在.m:

        tableView.layer.masksToBounds=YES;
        tableView.layer.borderWidth = 1.0f;
        tableView.layer.borderColor = [UIColor whiteColor].CGColor;
        

        【讨论】:

          【解决方案11】:

          使用PrettyKit 只需添加大约 5 行代码即可轻松完成此任务。如果您使用nib 文件或storyboard,也不要忘记申请this little hack。当您使用这种方法时,您应该从PrettyTableViewCell 继承您的单元格:

          #import <PrettyKit/PrettyKit.h>
          
          @class RRSearchHistoryItem;
          
          @interface RRSearchHistoryCell : PrettyTableViewCell
          

          这是我cellForRowAtIndexPath的例子:

          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
          {
            static NSString *cellIdentifier = @"RRSearchHistoryCell";
          
            RRSearchHistoryCell *cell = (RRSearchHistoryCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
          
            if ( cell == nil ) {
          
              NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RRSearchHistoryCell" owner:self options:nil];
              cell = topLevelObjects[0];
              cell.gradientStartColor = RGB(0xffffff);
              cell.gradientEndColor = RGB(0xf3f3f3);
          
            }
          
            RRSearchHistoryItem *item = _historyTableData[indexPath.row];
            [cell setHistoryItem:item];
          
          
            [cell prepareForTableView:tableView indexPath:indexPath];
          
            return cell;
          }
          

          【讨论】:

            猜你喜欢
            • 2014-09-15
            • 2011-08-23
            • 1970-01-01
            • 2011-02-03
            • 2012-07-10
            • 2011-01-29
            • 1970-01-01
            • 2015-09-10
            相关资源
            最近更新 更多