I am trying to implement a load more method to my last cell. After experimenting I finally could implement the load more (without function yet) to my cell. But it seems to be a bit harder, because I'm loading my content from a XML.
Here is what I do:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if([tabelle count]>= 10)
{
return [tabelle count]+1;
}
else
{
return [tabelle count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; }
if(indexPath.row != [tabelle count] )
{
cell.textLabel.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
cell.detailTextLabel.font = [UIFont fontWithName:@"Arial" size:12.0];
self.tableView.separatorColor = [UIColor clearColor];
NSString *TableText = [[NSString alloc] initWithFormat:@"%@", [[tabelle objectAtIndex:indexPath.row] number1]];
NSString *TableText2 = [[NSString alloc] initWithFormat:@"%@", [[tabelle objectAtIndex:indexPath.row] number2]];
NSString *cellValue = [NSString stringWithFormat:@"%@", TableText2];
NSString *cellValue2 = [NSString stringWithFormat:@"%@", TableText];
cell.textLabel.text = cellValue;
cell.detailTextLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
cell.detailTextLabel.text = cellValue2;
}
else if(indexPath.row==[tabelle count])
{
UILabel *loadMore =[[UILabel alloc]initWithFrame: CGRectMake(0,0,320,30)];
loadMore.textColor = [UIColor blackColor];
loadMore.backgroundColor = [UIColor clearColor];
loadMore.font=[UIFont fontWithName:@"Verdana" size:20];
loadMore.textAlignment=UITextAlignmentCenter;
loadMore.font=[UIFont boldSystemFontOfSize:20];
loadMore.text=@"Gimme more!";
[cell addSubview:loadMore];
}
return cell;
}
The load more cell (that reads Gimme more!) pops up on the last cell (but not creating a new last cell, just on the normal last cell, therefore interfering with its content) and also pops up randomly over the tableview.
The first problem was, that it bit itself with this method, so I had to alter it a little bit, otherwise I get the error below:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == [tabelle count]) {
} else {
NSString *text = [[self.tabelle objectAtIndex:indexPath.row] number1];
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 7, 1000.0f)];
return textSize.height + PADDING * 7;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
{
if(indexPath.row==[tabelle count])
{
[self.tableView cellForRowAtIndexPath:indexPath];
}
}
} And here is the error I had (and sometimes still have when scrolling through the view):
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]'
There seem to be some oddities with the code you posted. In the first snippet the if-statement seems to float by itself (no encapsulating method?). In heightForRowAtIndexPath you're not returning a value from the then section of the if-statement.
[EDIT] This is how I would write what you have posted so far:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// I am not the below make sense but that depends on what you are intending
// I do have a feeling there is a "off by one" issue here
if ([tabelle count] >= 10) {
return [tabelle count]+1;
}else{
return [tabelle count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.row != [tabelle count] ) {
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1"];
if (cell == nil) {
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell1"]; }
cell.textLabel.font = [UIFont fontWithName:@"Arial-Bold" size:14.0];
cell.detailTextLabel.font = [UIFont fontWithName:@"Arial" size:12.0];
cell.detailTextLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
self.tableView.separatorColor = [UIColor clearColor];
}
NSString *TableText = [[NSString alloc] initWithFormat:@"%@", [[tabelle objectAtIndex:indexPath.row] number1]];
NSString *TableText2 = [[NSString alloc] initWithFormat:@"%@", [[tabelle objectAtIndex:indexPath.row] number2]];
cell.textLabel.text = TableText;
cell.detailTextLabel.text = TableText2;
}else{
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell2"];
if (cell == nil) {
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell2"]; }
UILabel *loadMore = [[UILabel alloc] initWithFrame: CGRectMake(0,0,320,30)];
loadMore.textColor = [UIColor blackColor];
loadMore.backgroundColor = [UIColor clearColor];
loadMore.textAlignment = UITextAlignmentCenter;
loadMore.font = [UIFont boldSystemFontOfSize:20];
loadMore.text = @"Gimme more!";
[cell addSubview:loadMore];
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [tabelle count]) {
return 44.0;
}else{
NSString *text = [[self.tabelle objectAtIndex:indexPath.row] number1];
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 7, 1000.0f)];
return textSize.height + PADDING * 7;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == [tabelle count]) {
// TO-DO: user tapped 'Giimie more'
}else{
// TO-DO: user tapped a regular cell
}
}