Let me describe my scenario first.
I have a NSMutableDictionary
called self.wordDic
, which basically have some key
& value
like this: (This dictionary is dynamic, not fixed)
key value
---------------------------------------------------------
A (Apple, Aim, Arise, Attempt, Airplane, Absolute)
B (Bubble, Bite, Borrow, Basket)
C (Cat, Correct)
D (Dog, Direction, Distribute)
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self.wordListArray count] != 0)
{
self.wordDic = [self sortedDictionary:self.wordInWordListArray];
// Sorted key array
self.keyArray = [[NSArray alloc] init];
NSArray *key = [[NSArray alloc] init];
key = [self.wordDic allKeys];
self.keyArray = [key sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
}
I have an NSArray
called keyArray
where I have all of my NSDictionary
keys (Alphabetically). And then in cellForRowAtIndexPath
I show all my values of a particular key
(Alphabetically).
Here are my tableView
methods :
My tableView
:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.keyArray count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 28;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 28)];
// Add title label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100, 18)];
[titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14.0f]];
NSString *string = [self.keyArray objectAtIndex:section];
[titleLabel setText:string];
[view addSubview:titleLabel];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return kHomeTableViewCellHeight;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = [self.keyArray objectAtIndex:section];
NSMutableArray *value = [self.wordDic objectForKey:key];
return [value count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = kHomeTableViewCellID;
HomeTableViewCell *cell = (HomeTableViewCell *)[self.homeTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *secTitle = [self.keyArray objectAtIndex:indexPath.section];
secData = [self.wordDic objectForKey:secTitle];
[secData sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *data = [secData objectAtIndex:indexPath.row];
[cell.wordLabel setText:data];
return cell;
}
Everything works perfectly. But now I want to delete my rows.
- (void)tableView:(UITableView *) tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//Other code
..............
..............
// remove info from tableView array
[self.whichArraywillBeHere removeObjectAtIndex:indexPath.row];
[self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
In commitEditingStyle
with which array I should have to replace this self.whichArraywillBeHere array? Because, firstly I have no section in this method and secondly, I load my rows, alphabetically. So how can I know which array, should I used here and what will be it's indexPath
?
i think you're very close - you already have your keyArray sorted and stored in a property. You can get the values array from the dictionary with this - just like you did in the data source methods. The quick and dirty way to delete the right row/word would be to just sort it again.
-(void)tableView:(UITableView *) tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Other code
// ..
// remove info from tableView array
NSString *key = [self.keyArray objectAtIndex:indexPath.section];
NSMutableArray *words = [self.wordDic objectForKey:key];
[words sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[words removeObjectAtIndex:indexPath.row];
[self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Again this is the quick solution, note that you sort the array(s) each time a row is deleted and also anytime the table is reloaded (but if your word arrays are small this may not matter - that's up to you)