I have a tableView with 2 sections. I can fit 3 rows currently first 2 from first section and 1 row of second section. Whenever I receive a text from server I want a new row to be inserted at first place in second section. Like this:
--------- ---------
| 0-0 | | 0-0 |
--------- ---------
| 0-1 |------>| 0-1 |
--------- ---------
| 1-0 | |new 1-0|
--------- ---------
| 1-1 |
---------
I tried using insertRowsAtIndexPath and reloadSections method of table view but both of them does add a new row at start of section but also duplicated the rows of first section (2 in this case). Like this:
--------- ---------
| 0-0 | | 0-0 |
--------- ---------
| 0-1 |------>| 0-1 |
--------- ---------
| 1-0 | | 0-0 |
--------- ---------
| 0-1 |
---------
|new 1-0|
---------
| 1-1 |
---------
I add a new row on receiving notification via NSNotificationCenter here is the code fragment:
-(void)receiveNotification:(NSString *)notification
{
[myArray1 insertObject:notification atIndex:0];
[self.tblView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
// Alternate Code
// [self.tblView beginUpdates];
// [self.tblView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
// [self.tblView endUpdates];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return [myArray0 count];
else
return [myArray1 count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
static NSString *cellId = @"cell0";
Section0 *cell0 = [tableView dequeueReusableCellWithIdentifier:cellId];
//Manage first section cell
return cell0;
}
else
{
static NSString *cellId = @"cell1";
Section0 *cell1 = [tableView dequeueReusableCellWithIdentifier:cellId];
// Manage second section cell
return cell1;
}
}