0

I consumed JSON Data from my website to get Posts.

When i get JSON Data for 20 posts , i show that data in UITableView.

In my case , i want to add more one row in the last of tableView with String "Load More Posts." because when i tap that "Load More Posts" row , i need to reload my tableView to show another new posts.

I am using NSMutableArray and NSDictionary to get JSON Data and show Post in UITableView.

So how do i add it?

Fire Fist
  • 7,032
  • 12
  • 63
  • 109

2 Answers2

3
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView  = [[[UIView alloc] init] autorelease];
    footerView.backgroundColor = [UIColor clearColor];
    footerView.frame=CGRectMake(0, 0, 253,100);

    UIButton *button = [[UIButton alloc] init];
    [button setFrame:CGRectMake(0, 0, 252, 50)];
    [button setTitle:@"Load More" forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    [button setBackgroundColor:[UIColor clearColor]];
    [button.titleLabel setTextColor:[UIColor blackColor]];
    [button addTarget:self action:@selector(btn_LoadMore:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:button];
    tbl_location.tableFooterView.contentMode = UIViewContentModeScaleToFill;
    [button release];


    return footerView;
}

-(IBAction)btn_LoadMore:(id)sender
{
    tbl_location.hidden = TRUE;

    [self load_json_data];
}
-(void)load_json_data
{
    //some code

    [tbl_location reloadData];
    tbl_location.hidden = FALSE;
}
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
2

You can manullay handle the condition based on your number of records in Array. Here is I am giving you some sample code which would help you achieve what you want to do :

Here manage to return the number of rowns in cells, based on your records in Array.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if ( [jsonArray count] < 20 ) //20 Will change based on your number of records as loop progresses.
        {
                return [self.localJsonArray count];
        } else {
                return [self.localJsonArray count] + 1;
        }       
}

Fill the data in cells, from Array and check for condition

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        ImageCell *cell = (ImageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (indexPath.row != [localJsonArray count] ) { // As long as we haven’t reached the +1 yet in the count, we populate the cell like normal
                if (cell == nil) {
                        cell = [[[ImageCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
                } 
                NSDictionary *itemAtIndex = (NSDictionary *)[self.localJsonArray objectAtIndex:indexPath.row];
                [cell setData:itemAtIndex];
        } // Ok, all done for filling the normal cells, next we probaply reach the +1 index, which doesn’t contain anything yet
        if ( [jsonArray count] == 20 ) { // Only call this if the array count is 25
                if(indexPath.row == [localJsonArray count] ) { // Here we check if we reached the end of the index, so the +1 row 
                        if (cell == nil) {
                                cell = [[[ImageCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
                        }
                        // Reset previous content of the cell, I have these defined in a UITableCell subclass, change them where needed
                        cell.cellBackground.image = nil;
                        cell.titleLabel.text = nil;
                        // Here we create the ‘Load More Posts.’ cell
                        loadMore =[[UILabel alloc]initWithFrame: CGRectMake(0,0,362,73)];
                        loadMore.textColor = [UIColor blackColor];
                        loadMore.highlightedTextColor = [UIColor darkGrayColor];
                        loadMore.backgroundColor = [UIColor clearColor];
                        loadMore.font=[UIFont fontWithName:@"Verdana" size:20];
                        loadMore.textAlignment=UITextAlignmentCenter;
                        loadMore.font=[UIFont boldSystemFontOfSize:20];
                        loadMore.text=@"Load More Posts...";
                        [cell addSubview:loadMore];
                }
        }
        return cell;
}

And Lastly, maintain the event handling and differentiate between additional ROW with others :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if ( [jsonArray count] == 20 ) { //  Only call the function if we have 20 results in the array OR maintain its counts based on your array counts.
                if (indexPath.row == [localJsonArray count] ) {
                        NSLog(@"Load More requested"); // Add a function here to add more data to your array and reload the content
                } else {
                       NSLog(@"Normal cell selected"); // Add here your normal didSelectRowAtIndexPath code
               }
        } else {
                       NSLog(@"Normal cell selected with < 20 results"); //  Add here your normal didSelectRowAtIndexPath code
        }
}
Ajay Sharma
  • 4,509
  • 3
  • 32
  • 59
  • @why negative for this ? Can anybody let me know , whats wrong with this solution ? Yahiko Have you tried this in your code.... – Ajay Sharma Feb 19 '14 at 12:04
  • sorry bro I was going out. I just vote up and vote down is not me. Let me check it. Thanks :) – Fire Fist Feb 19 '14 at 15:15