2012-03-06 118 views
-1

我創建了一個包含標籤和textfield的自定義單元格。我在表格視圖中使用此自定義單元格。當我在文本字段中輸入數據,當我滾動視圖時,我的數據正在被替換。對於每個滾動它被替換/擦除。任何一個告訴我,我在哪裏做錯誤。 我的自定義單元代碼在這裏。謝謝!無法重用表格視圖中的自定義單元格

-(UITableViewCell *)returnCellForEach:(UITableView *)tableView 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"]; 
    if (cell == nil) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"CustomCellToAddDetails" owner:self options:nil]; 
     cell = customCell; 
//  customCell = nil; 
     customLabel = (UILabel *)[customCell.contentView viewWithTag:100]; 
    } 
    return cell; 
} 

-(UITextField *)dataField 
{ 
    UITextField *textField = customField; 

    return textField; 
} 
+0

什麼是'returnCellForEach替換此行

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"]; 

:'和你在哪裏打電話呢? – jrturton 2012-03-06 06:54:18

+0

@jrturton:這是我的自定義方法。我爲自定義單元格創建了它。 – Kiran 2012-03-06 07:01:23

+0

_你從哪裏叫它?你的cellForRowAtIndexPath方法在哪裏? – jrturton 2012-03-06 07:14:22

回答

0

您將需要通過用戶輸入的數據存儲的地方與特定indexpath和當您通過細胞滾動,你首先需要檢查是否有對應於indexpath任何dataObject時,並設置相應的數據。您可以使用字典來存儲與特定單元格對應的數據。希望它可以幫助

+0

謝謝!其實我正在做一個生日剩餘應用程序,它與數據庫一起工作。我將所有這些文本字段值存儲在數據庫中。同樣,如果我使用字典,它會在保存數據庫中的數據時產生任何問題? – Kiran 2012-03-06 05:29:02

+0

沒有問題,你可以安全地使用數據庫,但是當你使用tableview和你滾動表格時,你可以使用字典臨時保存數據..你可以將這些數據保存到數據庫中並相應地獲取。 – 2012-03-06 06:04:12

0

我不熟悉您正在使用,在我的情況,我需要創建tableview中類外的自定義表格視圖單元格類的方法名,

類被稱爲QRTYPECELL和以下是相關代碼:

@implementation QRTypeCell 
@synthesize imageView; 
@synthesize labelview; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
if (self) { 
    // Initialization code 
    imageView = nil; 
    labelview= nil; 
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,30,30)]; 
    imageView.contentMode = UIViewContentModeCenter; 
    [self.contentView addSubview:imageView]; 
    self.contentView.backgroundColor = [UIColor blackColor]; 


    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    label.textColor = [UIColor greenColor]; 
    label.backgroundColor = [UIColor blackColor]; 

    label.font = [UIFont systemFontOfSize:17]; 
    self.labelview = label; 
    [self.contentView addSubview:label]; 


} 
return self;} 

然後在TableView中類:

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

QRTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[QRTypeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];} 
    CurrentQRCode = (QRCode *)[fetchedResultsController objectAtIndexPath:indexPath]; 
    NSString *icon = CurrentQRCode.parsed_icon; 
    NSString *string = CurrentQRCode.parsed_string; 
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;  
    NSString *filePath = [[NSBundle mainBundle] pathForResource:icon ofType:@"png"]; 
    UIImage *image_file = [UIImage imageWithContentsOfFile:filePath]; 
+0

:請回答我的問題。你的回答不適合我的問題。你給表格視圖的答案。我的問題部分是不同的。任何方式感謝您的幫助! – Kiran 2012-03-06 05:46:52

0
在IB

你看到一個標識符字段地方的任何值在並使用VAL作爲重用標識符。 我認爲這會幫助你。

+0

:我之前做過。並沒有改變。 – Kiran 2012-03-06 06:59:11

+0

您是否在 – hchouhan02 2012-03-06 07:02:14

+0

中放置了同名的「標識符」?我只把它放在自定義單元格IB中。只在自定義單元格中,我正在重新使用它。我是否還需要在其他地方使用? – Kiran 2012-03-06 07:25:54

0

好,我知道了你的customCell創建類,並與

CustomCell *customCell=(CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
相關問題