-1

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

- (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.

4

3 回答 3

6
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];

使用这个它会工作

于 2013-07-04T05:26:06.153 回答
1

您将不得不遍历所有其他 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"];

}
于 2013-07-04T05:20:03.863 回答
0

我已将委托、数据源设置为 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"];

}
于 2013-07-04T06:11:59.643 回答