-2

Hi everyone I would like to ask I want to show JSON data and customize the position for image and text in Table View. how can I do like that? please advice me.

You can take a look at the Image Url.

for image 1 it is the result as I've parsing from JSON.

Image 1

for image 2 it is my goal.

Image 2

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>

@property (retain, nonatomic) IBOutlet UITableView *myTableView;


@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *myObject;

    // A dictionary object
    NSDictionary *dict;

    // Define keys
    NSString *galleryid;
    NSString *name;
    NSString *titlename;
    NSString *thumbnail;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Define keys
    galleryid = @"GalleryID";
    name = @"Name";
    titlename = @"TitleName";
    thumbnail = @"Thumbnail";

    // Create array to hold dictionaries
    myObject = [[NSMutableArray alloc] init];

    NSData *jsonData = [NSData dataWithContentsOfURL:
                        [NSURL URLWithString:@"MY_JSON_URL"]];

    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];

    // values in foreach loop
    for (NSDictionary *dataDict in jsonObjects) {
        NSString *strGalleryID = [dataDict objectForKey:@"GalleryID"];
        NSString *strName = [dataDict objectForKey:@"Name"];
        NSString *strTitleName = [dataDict objectForKey:@"TitleName"];
        NSString *strThumbnail = [dataDict objectForKey:@"Thumbnail"];

        dict = [NSDictionary dictionaryWithObjectsAndKeys:
                strGalleryID, galleryid,
                strName, name,
                strTitleName, titlename,
                strThumbnail, thumbnail,
                nil];
        [myObject addObject:dict];
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return myObject.count;
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        // Use the default cell style.
        cell = [[[UITableViewCell alloc] initWithStyle : UITableViewCellStyleSubtitle
                                       reuseIdentifier : CellIdentifier] autorelease];
    }


    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];

    NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:data];
    cell.imageView.image = img;

    cell.textLabel.text = [tmpDict objectForKey:name];
    cell.detailTextLabel.text= [tmpDict objectForKey:titlename];

    //[tmpDict objectForKey:memberid]
    //[tmpDict objectForKey:name]
    //[tmpDict objectForKey:titlename]
    //[tmpDict objectForKey:thumbnail]

    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_myTableView release];
    [super dealloc];
}
@end
xDevelopers
  • 105
  • 1
  • 2
  • 9

1 Answers1

0

It will be easy if creating custom TableviewCell (subclass to UITableViewCell) to display the array of objects you have, than using UITableViewCell to display as you desired.

Create custom UITableviewCell with xib or with storyboard and create properties as you needed and give the position and styling for the cell elements.

Bind data for the cells in your cellForRowAtIndexPath method.

For more information, refer: this and this for more info

Community
  • 1
  • 1
Alex Andrews
  • 1,498
  • 2
  • 19
  • 33