2012-03-06 120 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]; 
相关问题