2015-05-21 34 views
0

我想永久更改选定的单元格数据,就像我在我的didSelectRowAtIndexPath方法中所做的那样,但问题是,当我选择一行时,单元格数据会发生变化,但是当我选择其他行时,前一个变成原样,而我还想要将数据保存到数组中,这些数组已被选中。这里是我的代码现在。如何永久更改选定的单元格数据?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
@try { 
    static NSString *cellidentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier]; 
    if(cell == nil) 
    { 
     NSArray *cellObjects = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil]; 
     cell = (UITableViewCell*) [cellObjects objectAtIndex:0]; 
    } 

    UILabel *label; 
    long row = [indexPath row]; 

    label = (UILabel *)[cell viewWithTag:10]; 

    label.text =time[row]; 
    label.textColor = [UIColor blackColor]; 
    cell.imageView.image = [img_clock_blue objectAtIndex:indexPath.row]; 
    cell.backgroundColor = [UIColor yellowColor]; 

    [tableView setSeparatorInset:UIEdgeInsetsZero]; 

    //int hecValue; 
    return cell; 
} 
@catch (NSException *exception) 
{ 
    NSLog(@"%@",exception); 
} 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
[tableView reloadData]; 
UITableViewCell *cell1 = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; 
cell1.imageView.image = [UIImage imageNamed:@"1_red.png"]; 
cell1.backgroundColor = [UIColor clearColor]; 
} 
+0

如果你需要保持原来选定的单元格,你需要保持它的轨道。现在你正在'didSelectRowAtIndexPath:'中重新加载表,所以之前选择的将被重新加载。 – Akhilrajtr

+0

@Nasir发生这种情况是因为你在每次选择道路后重新加载tableview只是删除行[tableView reloadData];并尝试... –

回答

0

尝试此,

  1. 创建视图控制器一个NSMutableArray@property。让说selectedIndexArray
  2. cellForRowAtIndexPath方法

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        //other codes 
        if ([self.selectedIndexArray containsObject:indexPath]) { 
         cell.imageView.image = [UIImage imageNamed:@"1_red.png"]; //assumes all selected cells have same image 
        } else { 
    
         cell.imageView.image = [img_clock_blue objectAtIndex:indexPath.row]; 
        } 
    
        .....//other code 
    } 
    

    通过self.selectedIndexArray = [[NSMutableArray alloc] init];

初始化viewDidLoad数组中didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.selectedIndexArray addObject:indexPath]; 
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
} 
+0

让我试试这个 –

+0

@Akhilrajti我也想保存在数组中选择了哪一行。 'self.selectedIndexArray'中的 –

+0

您有选定单元格的所有索引。 – Akhilrajtr

0

设置单元格内容的代码应该都在cellForRowAtIndexPath:之内。

您应该创建一个真实的数据模型来表示您的单元格的内容而不是time数组。使用诸如“time”和“selected”之类的属性创建一组自定义对象(或字典)。使用indexPath.row找到正确的对象,然后使用其“selected”属性来决定给它哪种图像。

didSelectRowAtIndexPath:设置“选择”是或否,并且根本不需要改变单元。

1

您正在修改的细胞,这是一个坏主意。你需要修改它获取数据的地方。

在您的didSelectRowAtIndexPath只需在数组中找到objectAtIndex:,将其修改为您的意愿,然后重新加载表。

例如,如果只有标题(NSStrings),则字符串数组就足够了。但大多数时候它不会,因为你显示的是自定义的东西。

看起来你在这里没有自定义的类,所以我只做一个例子,你可以很容易地翻译。假设您试图显示Animal对象的列表。

创建您的Animal继承自NSObject的类。 (新文件,课程等)。

添加你需要在Animal.h文件的属性,例如

@property (weak, nonatomic) NSString *name; 
@property (nonatomic) int size; 
@property (nonatomic) int weight; 
@property (weak, nonatomic) NSString *countryOfOrigin; 

您还可以在技术上需要一个类来创建/管理/读取/保存这些Animal对象,但让我们保持它的简单,做它在你的控制器的viewDidLoad

- (void)viewDidLoad{ 
    [super viewDidLoad]; 

    Animal *myAnimal = [[Animal alloc]init]; 
    myAnimal.name = @"Lion"; 
    myAnimal.size = 13; 
    myAnimal.weight = 100; 
    myAnimal.countryOfOrigin = @"NoIdeaHahahah"; 

    // You can hardcode a couple like that, and add them to your array used for your tableview data. Basically we just want some of your custom objects in an array, for your tableview. 
    } 

好吧,现在我们有一个动物(我们的数据)为您的tableview数组。你可以用它来创建你的行。

cellForRow创建细胞,简单地与启动:

Animal *animal = [myArray objectAtIndex:indexPath.row]; 

再喂你的细胞与动物

cell.titleLabel.text = animal.name; 

例如的属性。

而且在didSelect可以修改特定的动物,就像我在这个答案:)

Animal *animal = [myArray objectAtIndex:indexPath.row]; 
animal.name = @"IJustChangedTheName"; 

[self.tableView reloadData]; 

这一切是常见的做法,但我们在viewDidLoad做的最开始说,这是非常残酷,但我相信你可以适应你的代码:)