Reputation: 881
I want to change the selected cell data permanently as i have done in my didSelectRowAtIndexPath
method but the problem is that when I select a row the cell data is change but when i select any other row the previous become as it was, and I also want to save rows in an array, those been selected in an array. here is my code right now.
-(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];
}
Upvotes: 0
Views: 194
Reputation: 6038
You're modifying the cell, which is a bad idea. You need to modify the place where it's getting its data.
in your didSelectRowAtIndexPath
just find the objectAtIndex:
in the array, modify it to your will, then reload the table.
If you only have, for example, titles (NSStrings
), then an array of strings will suffice. But most of the time it won't, because you're displaying something custom.
it looks like you don't have a custom class here, so I'll just make an example that you can translate easily. Let's say you're tryign to display a list of Animal
objects.
Create your Animal
class inheriting from NSObject. (New file, class, and so on).
Add the properties you will need in the Animal.h
file, for example
@property (weak, nonatomic) NSString *name;
@property (nonatomic) int size;
@property (nonatomic) int weight;
@property (weak, nonatomic) NSString *countryOfOrigin;
You'll also technically need a class to create/manage/fetch/save these Animal
objects but let's keep it simple and do it in the viewDidLoad
of your controller.
- (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.
}
Ok so now we have an array of Animal (our data) for your tableview. You can use that to create your rows.
When creating the cell in the cellForRow
, simply start with :
Animal *animal = [myArray objectAtIndex:indexPath.row];
and then feed your cells with the properties of that animal
cell.titleLabel.text = animal.name;
for example.
And in the didSelect
you can modify that specific animal, like I said at the very beginning of this answer :)
Animal *animal = [myArray objectAtIndex:indexPath.row];
animal.name = @"IJustChangedTheName";
[self.tableView reloadData];
All this is common practice, except what we did in the viewDidLoad
that is very brutal, but I'm sure you'll be able to adapt that to your code :)
Upvotes: 1
Reputation: 5182
Try this,
NSMutableArray
@property
in view controller. lets say selectedIndexArray
viewDidLoad
by self.selectedIndexArray = [[NSMutableArray alloc] init];
in cellForRowAtIndexPath
method
-(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
}
in didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.selectedIndexArray addObject:indexPath];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
Upvotes: 0
Reputation: 31016
The code for setting up cell contents should all be in cellForRowAtIndexPath:
.
You should create a real data model to represent the contents of your cells instead of the time
array. Create an array of custom objects (or dictionaries) with properties such as "time" and "selected". Use indexPath.row
to find the correct object and then use its "selected" property to decide which kind of image to give it.
didSelectRowAtIndexPath:
sets "selected" YES or NO and doesn't need to change the cell at all.
Upvotes: 0