ios - 当复选框被选中时,不会调用 didDeselectRowAtIndexPath
<p><p>我正在使用带有复选框的 UITableView。在我的 <code>cellForRowAtIndexPath</code> 方法中,我将选定的几个单元格设置如下,</p>
<pre><code>UITableViewCell *cell = ;
if(tableView == self.sideBarTbl){
cell.textLabel.text = ;
cell.tag = indexPath.row;
if(indexPath.row == 0){
;
}else if(indexPath.row == 1){
;
}else {
;
}
}
</code></pre>
<p>简而言之,如果该行是 0,1,那么我检查 UITableView 的复选框。</p>
<p>当用户点击第 0 行或第 1 行时,它应该调用 <code>didDeselectRowAtIndexPath</code>,因为第 0 行和第 1 行的复选标记已经被选中。但是,它总是调用 <code>didSelectRowAtIndexPath</code>。不管复选框是否被选中,它总是调用<code>didSelectRowAtIndexPath</code>。如何让它在选中复选框时调用 <code>didDeselectRowAtIndexPath</code> 并在未选中复选框时调用 <code>didSelectRowAtIndexPath</code> ?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><code>didDeselectRowAtIndexPath</code> 方法只会为之前被触摸选择的单元格调用。它与单元格的 <code>selected</code> 属性无关。如果你想对取消选择的单元格做一些事情,只需使用 <code>didSelectRowAtIndexPath</code> 方法在 NSMutableArray 中手动记录选择。忘记 <code>didDeselectRowAtIndexPath</code>。供引用:<a href="https://stackoverflow.com/questions/44281577/uitableview-cell-on-double-click-should-go-to-previous-state/44281778#44281778" rel="noreferrer noopener nofollow">UITableView cell on double click should go to previous state</a> </p>
<p>还有一个 Objective-C 示例:</p>
<pre><code>@interface SomeViewController : UIViewController<UITableViewDelegate> {
NSMutableArray *selectedIndexPaths;
}
@implementation SomeViewController
//......
- (void)viewDidLoad {
;
selectedIndexPaths = ;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if () {
;
//Do something when deselecting.
}
else{
;
//Do something when selecting.
}
}
//......
@end
</code></pre>
<p>如果选择是单选独占的,比如单选框,只需在处理前一个取消选择的过程后,使用单个变量<code>NSIndexPath *selectedIndexPath;</code>在<code>didSelectRowAtIndexPath</code>中记录选择选定的单元格。</p></p>
<p style="font-size: 20px;">关于ios - 当复选框被选中时,不会调用 didDeselectRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/44340983/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/44340983/
</a>
</p>
页:
[1]