我有一個非常簡單的應用程序與UITableViewController。編輯完成後,我試圖將一行滑入第一部分的位置0。新行應具有INSERT編輯樣式,而現有行應具有DELETE樣式。如何使用tableView:editingStyleForRowAtIndexPath?
我已經覆蓋了以下4種方法:
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.editing && section == 0) {
return2;
}
return 1;
}
- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView
editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath {
int section = indexPath.section;
int row = indexPath.row;
if (self.editing && section == 0 && row == 0) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView beginUpdates];
if (editing) {
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:ip]
withRowAnimation:UITableViewRowAnimationLeft];
} else {
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:ip]
withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView endUpdates];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ClientsControllerCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc ] initWithStyle:UITableViewCellStyleValue1reuseIdentifier:CellIdentifier] autorelease];
}
int section = indexPath.section;
int row = indexPath.row;
if (self.editing && section == 0 && row == 0) {
cell.textLabel.text = @"Add Me";
cell.detailTextLabel.text = @"Detail text";
} else {
cell.textLabel.text = @"Test me";
cell.detailTextLabel.text = @"Detail text";
}
return cell;
}
但只要我進入編輯模式,「既」細胞落得UITableViewCellEditingStyleInsert的編輯風格。
如果我改變自己的邏輯並將新的單元格追加到END - 那麼它就會以DELETE樣式正確地繪製單元格,並且新單元格將得到一個INSERT。
無論哪種方式,tableView:editingStyleForRowAtIndexPath被調用4次。實際上,如果我將新單元格插入到section:0 row:0中,則該方法將被調用:section 0:0,0:1,0:0,0:0。而如果我將新的單元格追加到section:row:1中,則此方法被調用:section 0:0,0:1,0:0,0:1。
我錯過了什麼?我應該可以插入一行,並抓住它的權利?出於某種原因,我無法再看到section = 0 row = 1。
-Luther