HI .. 我有一个Tableview,每个单元格都有一个按钮,我以编程方式添加该按钮现在我想要在按下该按钮时删除单元格
任何人都帮助我...
答案 0 :(得分:1)
以下是一个示例,希望对您有用:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return rowCount;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentity = @"removable";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentity];
if (!cell) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 70) reuseIdentifier:cellIdentity];
UIButton *removeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 40)];
[removeBtn setTitle:@"remove" forState:UIControlStateNormal];
[removeBtn setBackgroundColor:[UIColor blueColor]];
[removeBtn addTarget:self action:@selector(removeCell:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:removeBtn];
[removeBtn release];
}
return cell;
}
- (void)removeCell:(id)button {
UITableViewCell *cell = (UITableViewCell *)[(UIButton *)button superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSArray *removeRow = [NSArray arrayWithObject:indexPath];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:removeRow withRowAnimation:UITableViewRowAnimationFade];
rowCount--;
[self.tableView endUpdates];
}