2011-10-21 47 views
1

我有一个UITableView,我可以添加和删除单元格。每个单元格上还有两个按钮,用于从单元格文本中加减1。但是当我转到不同的页面,然后返回到表格视图页面时,所有单元格文本都会重新设置为1。但我希望单元格的文本保持在用户设置的值!有人能帮助我吗?我不知道该做什么。谢谢!保存表格视图的状态/保存表格视图文本的状态

回答

3

您将不得不保留一个NSMutableArray(可能为NSIntegers),该值保存单元格的值。然后,每当用户更改值时,更新数组中的值。此外,通过从数组中读取来显示单元格标签值。下面的示例代码 -

-(void)plusButtonClicked:(id)sender 
{ 
    //figure out the cell index which was updated using sender 
    //update the array entry 
    //[self.array objectAtIndex:index]++; self.array is the array you will maintain 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    cell.label.text = [NSString stringWithFormat: @"%d", [self.array objectAtIndex:indexPath.row]]; 
} 

如果您希望即使在终止并重新启动应用程序后仍然保留这些值,请考虑使用CoreData。

+0

你知道关于如何使用核心数据保存表视图状态的好教程吗?我搜索了,我无法找到有用的东西!谢谢您的帮助! – iProRage