[英]Set UITableViewCell selected to YES in willDisplayCell: and the cell can't be deselected anymore
I want to pre-select some rows in a multiple selection UITableView. 我想在多选UITableView中预先选择一些行。
I do this in tableView:willDisplayCell:forRowAtIndexPath:
by simply setting [cell setSelected:YES animated:NO];
我在
tableView:willDisplayCell:forRowAtIndexPath:
执行此操作tableView:willDisplayCell:forRowAtIndexPath:
只需设置[cell setSelected:YES animated:NO];
. 。
However, for some reason this disables deselection for these rows. 但是,由于某种原因,这会禁用对这些行的取消选择。 The embedded controls still work, and so do detail disclosure buttons.
嵌入式控件仍然有效,细节公开按钮也是如此。
I have uploaded a sample project here: https://github.com/leberwurstsaft/TableViewIssue where you can check out the behavior (in Viewcontroller.m
lines 49-51). 我在这里上传了一个示例项目: https : //github.com/leberwurstsaft/TableViewIssue ,您可以在其中查看行为(在
Viewcontroller.m
第49-51行)。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setSelected:NO animated:NO]; // --> setSelected:YES and the cells can't be deselected anymore...
}
What seems to be the problem? 什么似乎是问题?
In addition to setting the cell as selected, you also need to inform the tableView that the cell is selected. 除了将单元格设置为选中之外,还需要通知tableView已选择单元格。 Add a call to
-tableView:selectRowAtIndexPath:animated:scrollPosition:
to your willDisplayCell:
method: and you will be able to deselect it as normal. 添加对
-tableView:selectRowAtIndexPath:animated:scrollPosition:
的调用-tableView:selectRowAtIndexPath:animated:scrollPosition:
到你的willDisplayCell:
方法:你可以正常取消选择它。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (/*should be selected */) {
[cell setSelected:YES animated:NO];
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
This is similar to @Ivan Loya's solution, but can be done in the same method you were already using. 这类似于@Ivan Loya的解决方案,但可以使用您已经使用的相同方法完成。 Be sure to use
UITableViewScrollPositionNone
to avoid odd scrolling behavior. 一定要使用
UITableViewScrollPositionNone
来避免奇怪的滚动行为。
try this in view did appear, work for me 尝试这个在视图中确实出现,为我工作
- (void)viewDidAppear:(BOOL)animated {
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[_tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}
in your viewController.h add and link it to the table view 在viewController.h中添加并链接到表视图
@property (weak, nonatomic) IBOutlet UITableView *tableView;
and comment the line in willDisplayCell Function 并在willDisplayCell函数中注释该行
I think you should put your selection in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
and toggle in there the selection. 我认为你应该把你的选择放在
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
并在那里切换选择。
Here is what I do in a similar situation where I want to toggle the checkmark on each row 这是我在类似的情况下做的,我想在每一行上切换复选标记
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:NO]; UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; Person * person = [self.contactTable objectAtIndex:indexPath.row]; if (newCell.accessoryType == UITableViewCellAccessoryNone) { newCell.accessoryType = UITableViewCellAccessoryCheckmark; person.isMatch = [NSNumber numberWithBool:YES]; self.countChecked ++ ; }else { newCell.accessoryType = UITableViewCellAccessoryNone; person.isMatch = [NSNumber numberWithBool:NO]; self.countChecked -- ; } }
For those wondering in iOS 11 how to fix this: 对于那些想知道在iOS 11中如何解决这个问题:
Drew C, Got it right, But Im translating it to Swift: Drew C,说得对,但我把它翻译成Swift:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.isSelected {
cell.isSelected = true
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.