gpt4 book ai didi

iphone - 如何使用 UITableViewCellAccessoryCheckmark 取消选中所有行

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

我有一个 UITableView,每行都包含一个使用 UITableViewCellAccessoryCheckmark 的复选框。我不知道如何使用 didSelectRowAtIndexPath 方法取消选中所有复选框。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  

UITableViewCell *oldCell;

int count = [self.myTableRowNamesArray count];

for (NSUInteger i = 0; i < count; ++i) {
// Uncheck all checkboxes
// OF COURSE THIS DOESN'T WORK
// BECAUSE i IS AN INTEGER AND INDEXPATH IS A POINTER
FOO: oldCell = [myTableView cellForRowAtIndexPath:(int)i];
// GOOD CODE:
oldCell = [penanceOptionsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}

最佳答案

我建议不要修改didSelectRowAtIndexPath:中所有单元格的.accessoryType,而是将选定的索引存储在某些ivar中,并更改数据源的 -tableView:cellForRowAtIndexPath: 方法中的 .accessoryType,即

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { 
self.selectedIndexPath = indexPath;
[tableView reloadData];
}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
...
cell.accessoryType = [indexPath compare:self.selectedIndexPath] == NSOrderedSame
? UITableViewCellAccessoryCheckmark
: UITableViewCellAccessoryNone;
...
}

这样,只有可见单元格会受到影响,屏幕外的数百万个其他单元格将不需要修改。

<小时/>

完全正确,这是在选择单元格的一般情况下在 Swift 中的完整实现。您可以在类中的其他地方使用您认为合适的 selectedIndexPath 。例如,在cellForRowAtIndexPath中选择适当的单元格原型(prototype)。

//  SelectingTableViewController

import UIKit

class SelectingTableViewController: UITableViewController
{
internal var selectedIndexPath:NSIndexPath? = nil

override func viewDidLoad()
{
super.viewDidLoad()
tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableViewAutomaticDimension

self.clearsSelectionOnViewWillAppear = false;
}

override func tableView
(tableView:UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath)
{
print("did select....")

// in fact, was this very row selected,
// and the user is clicking to deselect it...
// if you don't want "click a selected row to deselect"
// then on't include this clause.
if selectedIndexPath == indexPath
{
print("(user clicked on selected to deselect)")
selectedIndexPath = nil
tableView.reloadRowsAtIndexPaths(
[indexPath],
withRowAnimation:UITableViewRowAnimation.None)

tableView.deselectRowAtIndexPath(indexPath, animated:false)
return
}

// in fact, was some other row selected??
// user is changing to this row? if so, also deselect that row
if selectedIndexPath != nil
{
let pleaseRedrawMe = selectedIndexPath!
// (note that it will be drawn un-selected
// since we're chaging the 'selectedIndexPath' global)
selectedIndexPath = indexPath
tableView.reloadRowsAtIndexPaths(
[pleaseRedrawMe, indexPath],
withRowAnimation:UITableViewRowAnimation.None)
return;
}

// no previous selection.
// simply select that new one the user just touched.
// note that you can not use Apple's willDeselectRowAtIndexPath
// functions ... because they are freaky
selectedIndexPath = indexPath
tableView.reloadRowsAtIndexPaths(
[indexPath],
withRowAnimation:UITableViewRowAnimation.None)

}

}

关于iphone - 如何使用 UITableViewCellAccessoryCheckmark 取消选中所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5541328/

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