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

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:@"%d.jpg",i] forKey:@"Image"];[dic setValue:[NSString stringWithFormat:@"%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:messagedelegate:selfcancelButtonTitle:@"OK"otherButtonTitles: nil];[alert show];}@end

解析一下核心代码:而不去欣赏今天就开在我们窗口的玫瑰。

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

相关文章:

你感兴趣的文章:

标签云: