UITableView deselectrow在ipad应用程序中不起作用[关闭]

Posted

技术标签:

【中文标题】UITableView deselectrow在ipad应用程序中不起作用[关闭]【英文标题】:UITableView deselectrow not working in ipad app [closed] 【发布时间】:2013-07-04 05:02:23 【问题描述】:

我有表格视图,我想在选择时将图像添加到单元格,如果选择了新单元格,则将图像更改为选定单元格

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
      

          cell = [self.tableView cellForRowAtIndexPath:indexPath];

            cell.imageView.image=[UIImage imageNamed:@"Activity.png"]; 

        


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


       [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

       cell = [self.tableView cellForRowAtIndexPath:indexPath];


       cell.imageView.image=[UIImage imageNamed:@"Activity2.png"]; 
        NSUInteger row = indexPath.row;

  

这是我想给出的代码

        if (indexPath.row==0) 

       cell.imageView.image=[UIImage imageNamed:@"Activity2.png"]; 
       NSLog(@"Cell Clicked row  0");


       if(indexPath.row==1)


        NSLog(@"Cell Clicked row 1 in 0");

        cell.imageView.image=[UIImage imageNamed:@"Catalog"];

      

      
    if (indexPath.row==1) 

    cell.imageView.image=[UIImage imageNamed:@"Catalog2.png"]; 
    NSLog(@"Cell Clicked row  1");


    if(indexPath.row==0)


        NSLog(@"Cell Clicked row 0 in 1");


        cell.imageView.image=[UIImage imageNamed:@"Activity"];

    



new code added that how i want images on cell when new cell is selected.

【问题讨论】:

有什么问题?取消选择行不起作用是什么意思? 是的,我正在将图像分配给选定的第 0 行 Activity2.png,当我选择第 1 行时,我希望第 0 行应该有 Activit,ong,第 1 行应该有 Row2.png 【参考方案1】:
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];

用这个就行了

【讨论】:

【参考方案2】:

你必须遍历所有其他 TableViewCells 并设置它们的图像,试试这个。

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


    [aTableView deselectRowAtIndexPath:indexPath animated:YES];

    NSUInteger row = indexPath.row;
    for (int i=0; i<=4; i++) 
        UITableViewCell *cell = [aTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:indexPath.section]];

        cell.imageView.image=nil;
    
    UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
    cell.imageView.image=[UIImage imageNamed:@"Default.png"];


【讨论】:

我希望选择第一行,它应该显示 Activity2.png,如果选择了第 2 行,那么它可能会显示 Catalog2,而 row1 显示 Activity 而不是 activity2【参考方案3】:

我已将委托、数据源设置为 self 并为 iphone 和 iPad 使用了以下代码

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


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"imgres.jpeg"];


-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"images.jpg"];


【讨论】:

以上是关于UITableView deselectrow在ipad应用程序中不起作用[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

UITableView 自定义 Cell iPhone 使用 Swift 语言

在 UITableView 中显示 NSDictionary 字符串

从情节提要“Main”中实例化具有标识符“UIViewController-ZWG-5q-24I”的视图控制器,但没有获得 UITableView。

我在学习iOS的UITableView,为啥UITableView的最后一块单元格只能显示一半? [关闭]

根据 UIView 约束更新 UITableView 大小

取消选择 NSTableView 行时出现异常