如何在Table View单元格iOS中自定义位置

时间:2014-12-09 17:49:04

标签: ios objective-c

大家好我想问一下,我想在表视图中显示JSON数据并自定义图像和文本的位置。 我怎么能这样做?请指教。

您可以查看图片网址。

对于图像1,它是我从JSON解析的结果。

Image 1

对于图像2来说,这是我的目标。

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

1 个答案:

答案 0 :(得分:0)

如果要创建自定义的TableviewCell(UITableViewCell的子类)以显示您拥有的对象数组,那么将比使用UITableViewCell显示所需内容更容易。

使用UITableviewCellxib创建自定义storyboard并根据需要创建属性,并为单元格元素指定位置和样式。

cellForRowAtIndexPath方法中为单元格绑定数据。

有关详细信息,请参阅:thisthis了解详情