gpt4 book ai didi

iphone - 带有复选框的表格 View

转载 作者:行者123 更新时间:2023-12-03 20:28:36 25 4
gpt4 key购买 nike

我将此代码用于带有复选框的表格 View 。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

cell.accessoryType = UITableViewCellAccessoryNone;

cell.textLabel.text =@"a";
int flag = (1 << indexPath.row);
if (_checkboxSelections & flag)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}


return cell;
}



#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_checkboxSelections ^= (1 << indexPath.row);
[tableView reloadData];
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}

当我点击某个按钮时,如何知道哪些单元格被选中,哪些单元格未被选中?

最佳答案

您可以使用以下方法来访问按钮操作上的 tableView 单元格。您可以使用 if (cell.accessoryType == UITableViewCellAccessoryCheckmark) 条件检查选择情况,因为您正在为所选单元格设置 UITableViewCellAccessoryCheckmark。

- (void)onButtonClick {

int numberOfSections = [tableView numberOfSections];

for (int section = 0; section < numberOfSections; section++) {

int numberOfRows = [tableView numberOfRowsInSection:section];

for (int row = 0; row < numberOfRows; row++) {

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {

// Cell is selected

} else {

// Cell is not selected
}
}
}
}

关于iphone - 带有复选框的表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6302732/

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