2012-02-02 44 views
5

Tôi có UITableViewCell phù hợp trên UITableView, nhưng bằng cách nào đó tôi không thể thay đổi màu nền khi vào chế độ chỉnh sửa phong cách .. Tôi đã thử tất cả mọi thứ trên web, tìm kiếm tất cả ngày dài, nhưng vẫn không thể sửa nó (tôi đã tìm kiếm StackOverflow). làm ơn giúp tôi.không thể thay đổi màu nền UITableViewCell

 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"MainTableViewCell" owner:nil options:nil]; 

     for (UIView *view in views) { 
      if([view isKindOfClass:[UITableViewCell class]]) 
      { 
       cell = (MainTableViewCell *)view; 
      } 
     } 
    } 

Trả lời

18

Hãy thử tùy biến các tế bào bên trong tableView: willDisplayCell: phương pháp, một cái gì đó như thế này:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    cell.backgroundColor = [UIColor redColor]; 

} 
+0

nó hoạt động hoàn hảo người đàn ông! cảm ơn! :) –

+0

vui vì tôi có thể trợ giúp –

0

thử thiết lập các tế bào bất động sản backgroundColor, làm việc cho tôi cell.backgroundColor=[UIColor blueColor];

+0

của người đàn ông không làm việc .. có thể bạn có một giải pháp khác? –

+0

@YossiTsafar xin lỗi, hãy xem bản chỉnh sửa của tôi – Daniel

2

có toàn quyền kiểm soát trên các tuỳ biến của các tế bào của bạn, tôi thích tốt hơn để ghi đè lên các tế bào xem,

tạo ra một lớp mới cho chế độ xem ô, được gọi bằng UITableView,

sau này khi tôi truy cập máy tính công việc của mình nếu bạn không tìm thấy câu trả lời, tôi sẽ đăng một số mã mẫu,

khi bạn xem làm thế nào nó hoạt động là khá dễ dàng

bạn cũng có thể đặt một hình ảnh cho nền di động của bạn, và đặt nhãn khác nhau và hình ảnh, các nút, textfields ở những nơi phong tục của di động của bạn,

EDIT >>

mã! [Là quá mức cần thiết chỉ để thay đổi nền, nhưng nếu bạn muốn thực sự tùy chỉnh di động của bạn, đây là con đường để đi!]

trong CustomCell.h bạn

#import <UIKit/UIKit.h> 
@interface CustomCell : UITableViewCell { 
UILabel *_kLabel; 
UILabel *_dLabel; 
} 
@property (nonatomic, retain) UILabel *kLabel; 
@property (nonatomic, retain) UILabel *dLabel; 
- (void) initLabels; 
@end 

trong CustomCell.m bạn

#import "CustomCell.h" 
@implementation CustomCell 
@synthesize kLabel = _kLabel; 
@synthesize dLabel = _dLabel; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
if (self) { 
    // Initialization code 

    CGRect popUpImageBgndRect = CGRectMake(0, 0, 942, 44); 
    UIImageView *popUpImageBgnd = [[UIImageView alloc] initWithFrame:popUpImageBgndRect]; 
    [popUpImageBgnd setImage:[UIImage imageNamed:@"tableCellBgnd.png"]]; 
    popUpImageBgnd.opaque = YES; // explicitly opaque for performance 
    [self.contentView addSubview:popUpImageBgnd]; 
    [popUpImageBgnd release]; 

    [self initLabels]; 
    } 
    return self; 
    } 

    - (void)layoutSubviews { 

[super layoutSubviews]; 

CGRect contentRect = self.contentView.bounds; 

CGFloat boundsX = contentRect.origin.x; 

CGRect frame; 


frame= CGRectMake(boundsX+10 ,10, 200, 20); 

self.kLabel.frame = frame; 


frame= CGRectMake(boundsX+98 ,10, 100, 20); 

self.dLabel.frame = frame; 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
[super setSelected:selected animated:animated]; 

// Configure the view for the selected state 
} 

    - (void) initLabels { 
self.kLabel = [[[UILabel alloc]init] autorelease]; 
self.kLabel.textAlignment = UITextAlignmentLeft; 
self.kLabel.backgroundColor = [UIColor clearColor]; 
self.kLabel.font = [UIFont fontWithName:@"FS Albert" size:16]; 
self.kLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1]; 

self.dLabel = [[[UILabel alloc]init] autorelease]; 
self.dLabel.textAlignment = UITextAlignmentLeft; 
self.dLabel.backgroundColor = [UIColor clearColor]; 
self.dLabel.font = [UIFont systemFontOfSize:16]; 
self.dLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1]; 

[self.contentView addSubview:self.kLabel]; 
[self.contentView addSubview:self.dLabel]; 

} 

-(void) dealloc { 

[_kLabel release]; 
[_dLabel release]; 

[super dealloc]; 
} 

    @end 

Và trong ViewController.m bạn

YourViewController.m 

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

static NSString *CellIdentifier = @"Cell"; 



CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 

    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    } 


    return cell; 

    } 

ENJOY !! ;)

+0

ok người đàn ông, cảm ơn! –

+0

@YossiTsafar kiểm tra chỉnh sửa !, với image.png làm nền, tôi nghĩ là linh hoạt hơn, – MaKo

+0

Cảm ơn rất nhiều người !!! :) –

0

Vui lòng thử này, nó làm việc cho tôi.

UIView *bgView = [[UIView alloc] initWithFrame:cell.frame]; 
bgView.backgroundColor = [UIColor greenColor]; 
cell.backgroundView = bgView; 
[bgView release]; 
4

Bạn cần thay đổi nội dung ô của ô XemView, chứ không phải thay đổi nền của ô.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 

    cell.contentView.backgroundColor = [UIColor redColor]; 
} 
0

Như @Westley đã đề cập;

Bạn cần thay đổi nội dung ô của ô XemView, chứ không phải chế độ xem nền của ô.

Đây là cách bạn nên làm điều đó:

if(index == conditionYouWant) 
    { 
     cell.contentView.backgroundColor = [UIColor whiteColor]; 
    } 
    else 
    { 
     cell.contentView.backgroundColor = [UIColor colorWithRed:0.925 green:0.933 blue:0.937 alpha:1.0]; 
    } 

Hy vọng nó giúp bạn guys ra

0

Hãy chắc chắn bạn không đã được thiết lập màu nền xem nội dung trong Bảng phân cảnh. Nếu bạn đã làm, thay đổi các tế bào.backgroundColor in code sẽ không tạo ra sự khác biệt, và bạn sẽ đi vòng quanh và lộn xộn (như tôi đã làm).

0
cell.backgroundColor = [UIColor redColor]; 
0

trong nhanh chóng 3 bạn có thể sử dụng

cell.backgroundcolor = UIColor.white 
Các vấn đề liên quan