gpt4 book ai didi

uitableview - 具有UIButton的TableviewCell

转载 作者:行者123 更新时间:2023-12-03 21:29:49 25 4
gpt4 key购买 nike

HI ..
我有一个Tableview,其中每个单元格都有一个按钮,我以编程方式添加了该按钮。现在,我想在按下该按钮时删除该单元格

有人请帮助我...

最佳答案

这是一个示例,希望对您有用:

- (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];


}

关于uitableview - 具有UIButton的TableviewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5468331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com