2015-12-30 21 views
0

當一個UITableView使用自定義單元格我得到一個奇怪的表重疊:使用UITableViewDataSource與定製單元有子視圖

問題

  • 向下滾動&最後兩行已經上兩行畫在他們身上!
  • 向上滾動&前兩排有兩行塗在它們上面!

下面是UITableViewDataSource代碼:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!; 

    let cellText:String = self.items[indexPath.row]; 

    let subjectField:UILabel = UILabel(frame: CGRect(x:55, y: 25, width: 303, height: 25)); 

    subjectField.text = cellText; 

    //cell.textLabel?.text = cellText; //works fine! 
    cell.addSubview(subjectField);  //fails XX(! 

    return cell; 
} 

下面是截圖:

  • : '0 9' 和 '1 A' 是油漆接管!
  • 參考: '0 9' 是列#0(0)和行#9('9')

enter image description here

問題

  • 爲什麼添加子視圖會導致繪畫問題?
  • 將自定義視圖繪製到單元格中的正確方法是什麼?

語境

  • 我真正發出代碼使用定製單元在一個自定義的tableview
  • 這裏此代碼示例原始代碼的精簡版。我可以發佈任何需要幫助解決這個問題!
+0

我看到你的代碼有兩個明顯的問題:1)'dequeueReusableCellWithIdentifier:'可能返回null,所以你需要創建一個新的cell實例。你最好使用'dequeueReusableCellWithIdentifier:forIndexPath:',它根據需要自動分配單元格,2)在重用單元時,你一次又一次地添加'subjectField'子視圖。您應該在XIB中添加一次,或者檢查它是否已經添加到您的代碼中。 –

回答

1

與小區標識符的問題必須是唯一的

FUNC的tableView(的tableView:UITableView的,的cellForRowAtIndexPath indexPath:NSIndexPath) - >的UITableViewCell {

let cellId:String = "Cell"+String(indexPath.row) 
    var cell = tableView.dequeueReusableCellWithIdentifier(cellId) 
    if (cell == nil){ 
     cell = UITableViewCell(style: .Default, reuseIdentifier:cellId) 
    } 
    let cellText:String = String(indexPath.row) 
    let subjectField:UILabel = UILabel(frame: CGRect(x:55, y: 25, width: 303, height: 25)) 

    subjectField.text = cellText 

    (cell! as UITableViewCell).addSubview(subjectField) 

    return cell! as UITableViewCell 

} 
+0

我被嚇了一跳,印象深刻和驚訝 - 你修好了!!!!!!!!!!! (cell == nil)是關鍵。感謝您在這裏溝通問題的來源,然後解決方案!結束了我的晚上,謝謝你! :) –

1

Dizzle ,創建自定義單元格和使用控制在運行時以及來自Xib。

1)在Xib或Storyboard中的ViewController中創建CustomCell。

2)給出的標識符的小區Give Identifier for your cell

3)的tableview你的cellForRowAtIndexPath數據源的方法添加以下代碼

static NSString *cellId = @"CustomIdenfier"; 
    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:firstCellId]; 
    if (cell == nil) { 
     cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     [self createCustomCell:cell atIndexPath:indexPath]; 
    } 
    [self updateCustomCell:cell atIndexPath:indexPath]; 
    return cell; 

4)createCustomCell方法如果在XIB添加控制則不需要這種方法

-(void)createCustomCell:(CustomCell*)cell atIndexPath:(NSIndexPath*)indexPath{ 
    //Here I am adding custom label run time, you can add 
    // Any control in Xib and create a reference IBOutlet for it and user here. 
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, cell.contentView.frame.size.width-10, 30)]; 
    lblTitle.textColor = [UIColor blackColor]; 
    lblTitle.tag = 1; 
    [cell.contentView addSubview:lblTitle]; 
} 

5)updateCustomCell方法

-(void)updateCustomCell:(CustomCell*)cell atIndexPath:(NSIndexPath*)indexPath{ 

    UILabel *lblTitle = (UILabel*)[cell.contentView viewWithTag:1]; 
    [lblTitle setText:[NSString stringWithFormat:@"%i",indexPath.row]]; 
} 

//編輯1 如果不使用Storyboard,則創建UITableviewCell的自定義擴展。

//CustomCell.h 
#import <UIKit/UIKit.h> 
@interface CustomCell : UITableViewCell 
@property (strong, nonatomic) UILabel *lblTitle; 
@end 


//CustomCell.m 
#import "CustomCell.h" 
@implementation CustomCell 
@synthesize lblTitle; 

- (void)awakeFromNib { 
    NSLog(@"imgUrl:%@ txtName:%@",imgUser,txtName); 
    // Initialization code 
} 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if(self){ 
     //Setup your Cell like initialising variables and setting frames of controls 
    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 
    // Configure the view for the selected state 
} 
@end 
+0

我不使用故事板。我是100%編程的,對不起!雖然謝謝你的詳細回答! –

+1

J-Dizzle,編程使用自定義單元編程。 – Anand

+0

感謝您的包容性編程! –

0

基於Rajesh Balam的答案這裏是我的原始代碼的工作形式從我的UITableViewDataSource!

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    /**********************************************************/ 
    /* @soln a UNIQUE 'cellId' is the key     */ 
    /**********************************************************/ 
    let cellId :String = "Cell" + String(indexPath.row); 

    var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId); 

    if (cell == nil){ 
     cell = UITableViewCell(style: .Default, reuseIdentifier:cellId); //comment this out and watch it fail! 
    } 

    let cellText:String = self.items[indexPath.row]; 

    let subjectField:UILabel = UILabel(frame: CGRect(x:55, y: 25, width: 303, height: 25)); 

    subjectField.text = cellText; 

    (cell! as UITableViewCell).addSubview(subjectField); 

    return cell! as UITableViewCell; 

} 

它的工作原理。謝謝Rajesh! :)

相關問題