gpt4 book ai didi

iphone - 从 MutableArray 加载的 UITableView 变成一个复选框列表

转载 作者:行者123 更新时间:2023-11-28 23:11:45 25 4
gpt4 key购买 nike

我有一个 TableView ,它是从下面列出的可变数组加载的。但是我需要为数组中的每个项目分配一个 ID 并在项目旁边有一个复选框。基本上它是我们搜索的偏好,它让用户根据勾选的复选框确定优先级。所以我想保存哪些项目被勾选到 plist 或类似的。

数组的加载方式如下:

arryTableIconsText =     [[NSMutableArray alloc] init]; 

[arryTableIconsText addObject:@"Facilities for partially sighted or blind people"];
[arryTableIconsText addObject:@"An 'assistance dogs welcome' policy"];
[arryTableIconsText addObject:@"Disabled access facilities for wheelchair users (with assistance)"];
*more items added here*

arryTableIcons = [[NSMutableArray alloc] init];

[arryTableIcons addObject:@"visuallyImpaired_off.png"];
[arryTableIcons addObject:@"guidedogs_off.png"];
[arryTableIcons addObject:@"wheelchairassist_off.png"];
*more items added here*

然后像这样加载到表中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];

cell.textLabel.text = [arryTableIconsText objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[arryTableIcons objectAtIndex:indexPath.row]];

return cell;
}

结果如下:

Icons

但我不知道从这里到哪里才能将其转换为保存 ID 的每个单元格右侧的复选框?

任何提示真的会很感激,汤姆

最佳答案

使用 NSMutableIndexSet 实例变量并用被检查的单元格的索引填充它。

然后在 cellForRowAtIndexPath 方法中,将单元格的附件类型设置为 UITableViewCellAccessoryTypeCheckmarkUITableViewCellAccessoryTypeNone ,具体取决于 indexPath.row 所在的位置NSMutableIndexSet 与否。

最后,当单元格被点击时,如果 indexPath.row 还没有被添加到索引集中,或者如果它已经存在则将其删除,以切换相应单元格的状态,然后调用 reloadData 在 tableView 上。

我在你的代码中也看到你不熟悉 UITableViewCells 的重用机制。您应该阅读 Apple 文档中的“Table View Programming Guide”并学习如何以更高效和 react 性的方式(在 react 性和内存占用方面)实现 cellForRowAtIndexPath


例子

// Let selectedCellIndexes be an instance variable in your .h of type NSMutableIndexSet*
// Initialize it (probably at the same place you initialise your texts & icons, once for all, probably in your init method
selectedCellIndexes = [[NSMutableIndexSet alloc] init];

然后填充单元格:

-(UITableViewCell*)tableView:(UITableView*)tv cellForRowAtIndexPath:(NSIndexPath*)indexPath {
// Try to recycle and already allocated cell (but not used anymore so we can reuse it)
UITableViewCell* cell = [tv dequeueCellWithReuseIdentifier:...];
if (cell == nil) {
// If we didn't manage to get a reusable (existing) cell to recycle it
// then allocate a new one and configure its general properties common to all cells
cell = [[[UITableViewCell alloc] initWithStyle:... reuseIdentifier:...] autorelease];

// ... configure stuff that are common to all your cells : lineBreakMode, numberOfLines, font... once for all
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
}

// Then here change the stuff that are different between each cell
// (this code will be executed if the cell has just been allocated as well as if the cell is an old cell being recycled)
cell.textLabel.text = [arryTableIconsText objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[arryTableIcons objectAtIndex:indexPath.row]];
cell.accessoryType = [selectedCellIndexes containsIndex:indexPath.row] ? UITableViewCellAccessoryTypeCheckmark : UITableViewCellAccessoryTypeNone;

return cell;
}

最后,切换复选标记:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([selectedCellIndexes containsIndex:indexPath.row]) {
[selectedCellIndexes removeIndex:indexPath.row];
} else {
[selectedCellIndexes addIndex:indexPath.row];
}
[tableView reloadData];
}

关于iphone - 从 MutableArray 加载的 UITableView 变成一个复选框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8051690/

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