首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何取消选中UITableView单元格?

如何取消选中UITableView单元格?
EN

Stack Overflow用户
提问于 2010-10-19 19:56:09
回答 19查看 203.7K关注 0票数 223

我正在做一个项目,我必须预先选择一个特定的单元格。

我可以使用-willDisplayCell预先选择一个单元格,但不能在用户单击任何其他单元格时取消选择它。

代码语言:javascript
复制
- (void)tableView:(UITableView*)tableView 
        willDisplayCell:(UITableViewCell*)cell
        forRowAtIndexPath:(NSIndexPath*)indexPath
{ 
    AppDelegate_iPad *appDelegte = 
      (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];

    if ([appDelegte.indexPathDelegate row] == [indexPath row])
    {
        [cell setSelected:YES];    
    } 
}

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

    AppDelegate_iPad *appDelegte = 
      (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];

    NSIndexPath *indexpath1 = appDelegte.indexPathDelegate;
    appDelegte.indexPathDelegate = indexPath;
    [materialTable deselectRowAtIndexPath:indexpath1 animated:NO];
}

你能帮上忙吗?

EN

回答 19

Stack Overflow用户

发布于 2010-10-19 20:04:20

使用此代码

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
 {
    //Change the selected background view of the cell.
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
 }

Swift 3.0:

代码语言:javascript
复制
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //Change the selected background view of the cell.
    tableView.deselectRow(at: indexPath, animated: true)
}
票数 427
EN

Stack Overflow用户

发布于 2012-05-24 05:18:51

在表视图委托的didSelectRowAtIndexpath中(或从任何位置)使用以下方法

代码语言:javascript
复制
[myTable deselectRowAtIndexPath:[myTable indexPathForSelectedRow] animated:YES];
票数 119
EN

Stack Overflow用户

发布于 2013-02-28 02:53:34

试试这个:

代码语言:javascript
复制
for (NSIndexPath *indexPath in tableView.indexPathsForSelectedRows) {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
票数 40
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3968037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档