您的位置:首页 > 产品设计 > UI/UE

iOS用远程的图片和txt数据对UITableView进行绑定

2013-10-23 12:34 281 查看
UITableView需要有一个数据源Array,但又不想把这个数据源的数据给写死,所以使用了一个可变的Array(NSMutableArray);

下面是ViewController.h的代码(主要在这里声明变量):

//
//  ViewController.h
//  use_table
//
//  Created by zengraoli on 13-10-22.
//  Copyright (c) 2013年 zeng. All rights reserved.
//

#import <UIKit/UIKit.h>

static NSString *const cellIdentifier = @"cell";

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>

@property(strong,nonatomic) UITableView *tableView;
@property(strong,nonatomic) UITableViewCell *tableViewCell;
@property(strong,nonatomic) NSMutableArray *allData;

@end


这是对应的ViewController.m文件:

//
//  ViewController.m
//  use_table
//
//  Created by zengraoli on 13-10-22.
//  Copyright (c) 2013年 zeng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize tableView = _tableView;
@synthesize tableViewCell = _tableViewCell;
@synthesize allData = _allData;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    //初始化表格
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    // 设置协议,意思就是UITableView类的方法交给了tabView这个对象,让完去完成表格的一些设置操作
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    //把tabView添加到视图之上
    [self.view addSubview:self.tableView];
    
    //    存放显示在单元格上的数据
    _allData = [NSMutableArray arrayWithCapacity:0];
    
    for (int i = 1; i < 5; i++)
    {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:[NSString stringWithFormat:@"http://192.168.1.117/Images/day1/%d.jpg",i] forKey:@"Image"];
        [dic setValue:[NSString stringWithFormat:@"http://192.168.1.117/Images/day1/%d.txt",i] forKey:@"content"];
        [_allData addObject:dic];
    }
}

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

// 返回表格视图呈现的数据一共有几部分(组),在通讯录的表格视图中看到的姓氏分组就是这个意思。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//返回行数,也就是返回数组中所存储数据,也就是section的元素
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_allData count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //声明静态字符串型对象,用来标记重用单元格
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    
    //用TableSampleIdentifier表示需要重用的单元
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    
    //如果如果没有多余单元,则需要创建新的单元
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableSampleIdentifier];
    }
    else
    {
        while ([cell.contentView.subviews lastObject ]!=nil)
        {
            [(UIView*)[cell.contentView.subviews lastObject]removeFromSuperview];
        }
    }
    
    UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,300,300)];
    bgImgView.image = [UIImage imageNamed:@"cell_album.png"];//加载入图片
    
    cell.backgroundView = bgImgView;
    
    //表视图单元提供的UILabel属性,设置字体大小
    cell.textLabel.font = [UIFont boldSystemFontOfSize:40.0f];
    
    //设置单元格UILabel属性背景颜色
    cell.textLabel.backgroundColor=[UIColor clearColor];
    
    //正常情况下现实的图片
    NSString *webImagePath = [[_allData objectAtIndex:indexPath.row] valueForKey:@"Image"];
    NSLog(@"%@", webImagePath);
    
    NSURL *imageUrl = [NSURL URLWithString:webImagePath];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
    
    [cell.imageView setFrame:CGRectMake(10, 10, 29, 29)];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(11, 10, 133, 72)];
    imageView.image = image;
    [cell.contentView addSubview:imageView];
    
    // 自定义文本信息
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(160, 25, 100, 20)];
    label.font = [UIFont boldSystemFontOfSize:10.0f];
    
    NSString *webContentPath = [[_allData objectAtIndex:indexPath.row] valueForKey:@"content"];
    NSLog(@"%@", webContentPath);
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:webContentPath]];
    NSString *webContent = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", webContent);
    
    label.text = webContent;

    [cell.contentView addSubview:label];
    
    if (image == nil)
    {
        NSLog(@"image is nil");
    }
    if (cell.imageView == nil)
    {
        NSLog(@"cell.imageView is nil");
    }

    return cell;
}

//设置单元格高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

//选中单元格所产生事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //首先是用indexPath获取当前行的内容
    NSInteger row = [indexPath row];
    //从数组中取出当前行内容
    NSString *rowValue = nil;
    NSString *message = [[NSString alloc]initWithFormat:@"You selected%@",rowValue];
    //弹出警告信息
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                   message:message
                                                  delegate:self
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles: nil];
    [alert show];
}

@end


解析一下核心代码:
UITableView里面都会有一个UIImageView,对应这个UIImageView,只是再创建出来一个大小位置相对固定的UIImageView,然后从网络链接中读取到jpg文件,对这个UIImageView上面的Image进行设置;当然这里也可以直接对本来UIImageView中Image进行设置,但是相对于大小,并不好控制而已。

下拉是从远程的txt中(对应的远程txt必须为utf-8格式,否则后面虽然得到了NSData,但是转换后的NSString数据却是nil,使用NSASCIIStringEncoding也是为乱码的)读取到内容,通过创建的UILable(已经设定好位置和字体等属性),然后设置这个UILable的内容为,刚才读取到的NSString;

最好把UILable加到UITableViewCell(代表每一行的单元格)中;

在web目录下面的文件:





下面是模拟器显示的效果:



下一步要完成的:

------点击某一个UITableViewCell的时候,打开到具体的某一个View中把信息(图集)和内容显示出来;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: