2011-04-06 36 views
1

我正在使用tabbar应用程序。也许这是一个微不足道的问题,但我只是简单地想要替换UITableViewCell的labelvalue。我从TextView获取NSString,嵌入在UITableView“输入”中。我想要修改的单元在“输入”视图的父视图内。 修改应该在保存功能中进行。
访问导航堆栈内不同UITableView的特定UITableViewCell

 
- (void)save:(id)sender 
{ 
    //Get the needed string 
    DescriptionViewCell *cell = (DescriptionViewCell *) [descriptionTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
    NSString *temp = [[NSString alloc]initWithString:[cell.textView text]]; 

    //Access parentview 
    FindTableViewController *rootController = [self.navigationController.viewControllers objectAtIndex:1]; 
    UITableViewCell *descriptionCell = [rootController.findUITable cellForRowAtIndexPath: [NSIndexPath indexPathForRow:0 inSection: 1]]; 

    //replace label string 
    descriptionCell.textLabel.text = temp; 
    [temp release]; 

    //return to parentview 
    [self.navigationController popViewControllerAnimated:YES]; 
} 


不幸的是,descriptionCell不包含任何数据,修改不会发生。你看到我的代码中有任何问题,或者我应该与索引“玩”吗?
在此先感谢。
干杯,
西蒙

+0

您是否尝试添加NSLog语句并打印出temp值? – Sabobin 2011-04-06 13:37:48

回答

0

其实我觉得你在descriptionCell却一无所获,因为该小区是不是在那个时候可见。如果单元格不可见,cellForRowAtIndexPath返回nil。看看文档 -

的cellForRowAtIndexPath:

Returns the table cell at the specified index path. 
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 
Parameters 

indexPath 

    The index path locating the row in the receiver. 

Return Value 

    An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range. 

You can do this by managing text in some data structure and when you go back to previous view you call reloadData and check for that value in cellForRowAtIndexPath. 
+0

Thx,做到了。但是数据必须在viewWillAppear方法中重新加载。 – 2011-04-06 15:31:29

+0

是的,你必须调用reloadData来查看表视图才会出现。 – saadnib 2011-04-06 16:10:30

0

你所能做的就是在你的根表视图控制器添加NSString财产将被用来存储更新文本的价值。然后在您的save方法中,使用新文本更新根视图控制器NSString属性的值。最后,你打电话给你的根视图controlles tableviews reloadData方法,像这样:

FindTableViewController *rootController = [self.navigationController.viewControllers objectAtIndex:1]; 
[rootController.tableView reloadData]; 

在要impliment你的表视图- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath方法根表视图,使其applys NSString的属性值到相应的单元格时,你请致电reloadData

if(indexPath.row == myCellIndex){ 
    cell.textLabel.text = myStringProperty; 
}