我正在使用自定義單元格爲UITableView
,並且上面有一個UIButton
,它應該從表格視圖中刪除當前單元格。 在cellForRowAtIndexPath
我做無法從UITableView中刪除自定義單元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CellIdentifier";
NSLog(@"Creating Cell");
FADynamicCell *cell= (FADynamicCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([FADynamicCell class])
owner:nil
options:nil] lastObject];
}
currentIndexPath = indexPath;
UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeCustom];
removeButton.tag = indexPath.row +100;
removeButton.frame = cell.removeButton.frame;
removeButton.backgroundColor = [UIColor colorWithRed:255./255 green:65./255 blue: 21./255 alpha:1];
removeButton.titleLabel.font = [UIFont systemFontOfSize:12];
removeButton.titleLabel.textAlignment = NSTextAlignmentCenter;
[removeButton setTitle:@"Remove" forState:UIControlStateNormal];
[removeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[removeButton addTarget:self action:@selector(removing) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:removeButton];
return cell;
}
而且在功能上正確的細胞去除
-(void) removing
{
[TOperations deleteTickerFromGroup:tickerGroupID andTickerSymbol:selectedSymbol];
NSNumber *selectedRowIndex1 = [NSNumber numberWithInteger:currentIndexPath.row];
[tableView1 beginUpdates];
NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedRowIndex1.integerValue-1 inSection:0];
[tableView1 endUpdates];
[tableView1 reloadData];
}
現在的問題是,它顯示的動畫,但我不能從表中刪除自定義單元格視圖。每當我再次加載視圖,即來自其他屏幕,它不顯示我點擊刪除的單元格。
任何幫助將不勝感激...
問題可能出在您的數據源中。檢查來自其他屏幕的項目是否仍然存在。 –
@Sviatoslav Yakymiv:當我從其他屏幕來的時候,它不存在,如果我從上面的代碼按下刪除按鈕 – Atul
@Atul,它似乎丟失了你的tableview的數據源。你應該使用一些數組/字典來跟蹤要創建的單元格以及哪些被刪除等。 –