2011-11-01 114 views
0

之前我问这个问题我搜索了很多,看到一些答案,但没有人可以帮助我,所以我决定再问一次,...感谢您的任何帮助自定义TableViewCell值滚动时消失

我知道有解决这个问题有两种方式,一种是存储在阵列tableviewcell值,另一个是避免重复使用它,但我可以NT实现它们,

我有一个具有内部一个文本框custome tableviewcell和一个按钮,当一个按钮点击一个添加另一个tableviewcell在tableview的底部;

当我实施第一个解决方案,我得到的tableviewcell从textfield.supperview这样的:

-(void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    NSLog(@"TextFileld Value : %@" , textField.text); 

    DivideTableViewCell *cell = (DivideTableViewCell *)textField.superview; 

    NSIndexPath *indexPath = [uiTableView indexPathForCell:cell]; 

    NSLog(@"index Path %i" , indexPath.row); 
} 

但每次indexPath.row是0,我可以NT更新我的数据源阵列....

,也很遗憾我无法实现第二个解决方案太...

所以感谢您的帮助:)

回答

0

如果你需要存储TableViewCells尝试以下操作。

拥有一个NSMutableDictionary实例变量并在TableViewController的init方法中分配/ init它(不要忘记在dealloc中释放它)。

- (id)init { 
    self = [super init]; 
    if (self) { 
    ivarDictionary = [[NSMutableDictionary alloc] init]; 
    } 
    return self; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    DivideTableViewCell *cell = [ivarDictionary objectForKey:[NSNumber numberWithInt:indexPath.row]]; 

    if (!cell) { 
    cell = [[DivideTableViewCell alloc] init]; 
    [iverDictionary setObject:cell forKey:[NSNumber numberWithInt:indexPath.row]]; 
    // Cell setup 1 
    } 

    // Cell setup 2 

    return cell; 
} 

从那里,你可以设置你需要在设置1范围内的小区,或者如果你想每次都做一些设置细胞就会被召回,然后使用安装程序2.

请注意,这在减少内存使用方面不是一个好主意,我不会用这种方法来处理20-30个以上的单元。

您仍然需要使用TextField委托方法来提取输入的文本,您可能还需要在每个单元格创建时分析indexPath变量,以便您可以知道正在编辑哪个文本字段。

+0

我想,这可能泄漏的细胞内存,您可能需要一个自动添加到DivideTableViewCell分配/初始化线 – Littlejon

+0

您好感谢您的关注,但不幸的是它NT工作对我来说,我不不知道我在哪里做错了什么,让我们改变目标,我可以从textFieldDidEndEditing获取文本,我如何获得该tableviewcell的textfielld值更改的索引路径? 我想如果我能得到正确的索引路径,我可以使我的数组数据源更新,一切都会好的。 谢谢 – ali

+0

@ali你需要添加一个ivar到你的DivideTableViewCell中,记录它显示的行。确保每次使用tableView显示单元格时更新此值:cellForRowAtIndexPath: – Littlejon

0

 
I Think you should store all the values of the text field in an array and replace with the changed value every time like i did,see my code ,

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomCell"; {
    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; for (id currentObject in topLevelObjects) { if ([currentObject isKindOfClass:[UITableViewCell class]]) { cell = (CustomCell ) currentObject; break; } } } cell.capitalLabel.text =[capitals objectAtIndex:indexPath.row]; cell.stateLabel.text = [states objectAtIndex:indexPath.row]; cell.t1.delegate=self; cell.t1.tag=indexPath.row; cell.t1.text=[arrTemp objectAtIndex:indexPath.row]; cell.s1.backgroundColor=[UIColor grayColor]; cell.s1.contentSize=CGSizeMake(1000, 40); return cell; } } /-(BOOL)textFieldShouldReturn:(UITextField *)textField { [arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text]; } */ -(void)textFieldDidEndEditing:(UITextField *)textField{ [arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text]; }

+0

我已经使用了自定义单元格! –