我正在嘗試構建用戶界面,以便用戶可以編輯核心數據實體的屬性。當用戶點擊編輯按鈕時,選擇一行將推動listDetailViewController
,這只是一個顯示屬性的表格視圖。它使用帶有標籤和UITextField的自定義表格視圖單元格。 listDetailViewController
正確顯示屬性,並會接受文本,但我不知道如何讓用戶輸入的文本保存。無法將編輯保存到核心數據屬性
如果我沒有解釋清楚,這裏是一個例子。我想更改列表的名稱,所以我點擊編輯,點擊列表,點擊列表名稱行,彈出鍵盤,輸入新名稱,點擊完成,然後彈出我回到RVC,沒有任何更改保存。我一直在這個問題上敲打我的頭幾天,並會喜歡一些幫助!
下面是相關的代碼從ListDetailViewController
:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(done)];
self.navigationItem.rightBarButtonItem = doneButton;
[doneButton release];
self.tableView.allowsSelection = NO;
self.tableView.allowsSelectionDuringEditing = NO;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 3;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *DetailCellIdentifier = @"DetailCell";
ListDetailCell *cell = (ListDetailCell *)[tableView dequeueReusableCellWithIdentifier:DetailCellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"ListDetailCell" owner:self options:nil];
cell = listDetailCell;
self.listDetailCell = nil;
// Configure the cell...
// list name
if (0 == indexPath.row) {
cell.label.text = @"List Name";
cell.textField.text = self.selectedList.listName;
cell.textField.placeholder = @"Name";
}
// Detail 1
if (1 == indexPath.row) {
cell.label.text = @"Detail 1";
cell.textField.text = selectedList.detail1;
cell.textField.placeholder = @"Detail 1";
}
// Detail 2
if (2 == indexPath.row) {
cell.label.text = @"Detail 2";
cell.textField.text = selectedList.detail2;
cell.textField.placeholder = @"Detail 2";
}
}
return cell;
}
- (void)done {
[self.listDetailCell resignFirstResponder];
[self.navigationController popViewControllerAnimated:YES];
}
label
和textField
在ListDetailCell
聲明,這是表格單元格筆尖我前面提到的高德。
我應該將委託添加到單元類,還是添加到'cellForRowAtIndexPath'方法?鍵盤不是問題,因爲完成按鈕和導航控制器按照他們應有的工作。爲了清楚起見,我編輯了我的示例以更清晰! – 2010-11-04 01:14:34
我上面更新了一些示例代碼。小心輸入錯誤,我把它輸入:-) – drekka 2010-11-04 02:38:34
這樣做,謝謝!我仍然圍繞着代表圍繞我的大腦,並且每一個新點都有所幫助。 :) – 2010-11-04 04:06:19