2

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;
    }
}
meteors
  • 1,747
  • 3
  • 20
  • 40
  • 5
    Could you share the code? – Javier Flores Font Apr 08 '15 at 11:03
  • 1
    This isn't supposed to happend so you are obviously using it wrong so share your code. Which functions you call and where? – Shamas S Apr 08 '15 at 11:06
  • 1
    @JavierFloresFont I cannot share code. I'll try make another demo code to replicate issue, till then can you share a demo code for same with me. – meteors Apr 08 '15 at 11:12
  • @iosDev82 I have a function which listens to notifications (from NSNotificationCenter) whenever that function gets called I add new string to the data array at index 0 which i use in cellForRowAtIndexPath and numberOfRowInSection. After that I tried both function (reload section and insertrow). I also tried tableView beginUpdates and endUpdates but out of luck. – meteors Apr 08 '15 at 11:16
  • 1
    I think your problem is related with the function numberOfRowsInSection. Ensure you are returning the correct number of rows after inserting an object. Or maybe you are adding more than 1 object in your datasource. It could be a lot of things. – Javier Flores Font Apr 08 '15 at 11:21
  • @JavierFloresFont I think he probably has an array of some sort whose count he returns in numberOfRowsInSection. That is why he is not facing the usual issue of 'number of rows not equal after insert'. So I would bet that he is adding the objects again in that array where he stores his objects. Wouldn't you agree? – Shamas S Apr 08 '15 at 11:29
  • @iosDev82 in numberOfRows i check the section and return array count. I have two separate array for both section. As for the storing part I have two methods one for populating each of data array during view did load and I add the string only where I receive notification. – meteors Apr 08 '15 at 11:37
  • Just put a log in numberOfRowsInSection. See how many rows are you returning after your reloadData. That should settle it. – Shamas S Apr 08 '15 at 11:38
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. – Hot Licks Apr 08 '15 at 12:46

2 Answers2

0

The above solution worked. Found that somewhere in viewDidLoad I was having extra call for reloading my datasource (the array) and tableView reloadData method besides the one in receiveNotification method. Removing the call in viewDidLoad solved the issue.

meteors
  • 1,747
  • 3
  • 20
  • 40
-2

Try this,

Before reload the table just replace your array as below:

 NSArray *copy = [mutableArray copy];
 NSInteger index = [copy count] - 1;

 for (id object in [copy reverseObjectEnumerator]) {
     if ([mutableArray indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) {
          [mutableArray removeObjectAtIndex:index];
 }
   index--;
}
jigs
  • 839
  • 1
  • 6
  • 23
  • This is very lengthy solution. This makes the table view also scroll slowly. I tried a similar solution before. – meteors Apr 08 '15 at 19:47