-2

I am creating an ios app in which i want to add each new row at the top in the UITableView but the already existing row should not be deleted.

For example i have a cell in row 1 when i click on it it will display the image the same image should be displayed even when we insert a new cell in row 1 and the old one moved to row 2.

I have searched it in stackoverflow but i could not understand what they have explained. So can anyone provide the tutorial or the sample application to insert, delete and update the row in UITableView in ios.

Thanks in advance.

Balaji Kondalrayal
  • 1,743
  • 3
  • 19
  • 38

2 Answers2

3

when you are adding new cell then Insert your cell data on ZERO index like this

NSMutableArray *dataArray = [[NSMutableArray alloc]initWithObjects:@"Hello",@"How Are You", nil]; 

// It's your data array that you have already allocation in viewDidLoad or some where else.. 


[dataArray insertObject:@"Joy" atIndex:0];  // Try this line on add there your are adding new cell data

[tableView beginUpdates];
NSIndexPath *indexpath = [NSIndexPath indexPathforRow:0 inSection:0];
NSArray *indextoadd = [NSArray arrayWithObject:indexpath1];
[tableView insertRowsAtIndexPaths:indextoadd withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];

Using this array will auto sort and "Joy" name will inserted on ZERO index and array auto shifted

after this Reload your tableview

[tableviewObj reloadData];

try this hope you will got success. For fully table Example UITableView example in iOS

Community
  • 1
  • 1
Jogendra.Com
  • 6,394
  • 2
  • 28
  • 35
2
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

These three methods you can use for delete,insert and update table row. Here Apple code for insertRowAtIndexPaths click here

Pradip Vanparia
  • 1,742
  • 10
  • 22