Fahad Ali Fahad Ali - 2 months ago 6
iOS Question

iOS Objective C : How to increase number of rows after given condition

I am very new to iOS, Living in a very small village with no help on iOS,
I need help in my code where I can increase number of rows once I reach to row number 20.


I actually have more than 6000 lines, when try to show all of them in tableview, it takes lot of time, so I want to load all rows in efficient way, like increasing it like +20, +50 etc

here is my code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return a;
}


and I am trying to increase rows like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if(indexPath.row == 20)
{
[_myTable reloadData];
[self.myTable beginUpdates];

a= a +50;
[self.myTable endUpdates];
}

if (indexPath.row > 60)
{
[self.myTable beginUpdates];
a= a +100;
[self.myTable endUpdates];
}
}


I declared myTable as property

I am sorry if my post is looking foolish I am at very basic position and sorry for my bad English.

Answer

The numbers of rows to be displayed in a tableView depends on data available i.e., the data to be displayed in the UITableView.

Let an array "Countries" containing list of 3 countries:

NSArray *countries;

countries  = @[@"India",@"USA",@"Germany",nil]; 

so, now the UITableView has 3 rows, so use this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return countries.count;
}

so if the countries list increased later, then it will work.

No need to increase number of rows in table manually.

If the response is from server, then use this:

NSArray *dataArray = [responseObject objectForKey:@"countriesJson"];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return dataArray.count;
}

Sample Project:

Please check my GitHub link below:

https://github.com/k-sathireddy/TableRowCountDynamic

Note: More images are loaded if we reach a certain limit.

Comments