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

IOS开发之UITableView

2015-07-23 10:41 253 查看
UITableView就相当于android中的listview,在这里先介绍一个简单的UItableView的用法,因为我也是刚学。而且也遇到了一些问题。直接上代码吧
这是ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) NSArray * fileList;
@property (nonatomic,retain) UITableView * tableView;

@end
这是ViewController.m文件
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
NSBundle * bundle = [NSBundle mainBundle];
NSString * plistPath = [bundle pathForResource:@"infoHeaderAndTitleAndDetile" ofType:@"plist"];
self.fileList=[[NSArray alloc] initWithContentsOfFile:plistPath];

UITableView * tView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
tView.delegate=self;
tView.dataSource=self;
_tableView=tView;
[self.view addSubview:_tableView];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellWithIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellWithIdentifier];
}
NSUInteger row = [indexPath row];
NSDictionary *rowDict = [self.fileList objectAtIndex:row];
cell.textLabel.text = [rowDict objectForKey :@"title"];
cell.imageView.opaque=YES;//must add this sentence,or the image will gone
cell.imageView.image = [UIImage imageNamed:[[rowDict objectForKey :@"header"] stringByAppendingString:@".jpg"]];//image must have the .jpg or other
NSLog(@"%@",cell.imageView);

cell.detailTextLabel.text = [rowDict objectForKey :@"detail"];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

return cell;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.fileList count];
}

@end
需要注意的地方有三点,也是我遇到的问题
1、两个必须实现的协议方法,相当于java的接口的抽象方法
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
2、图片不能正常显示的原因
cell.imageView.opaque=YES;//must add this sentence,or the image will gone
这个opaque属性要设为YES,默认是NO
3、图片无需写路径,但是后缀名一定要写,比如.jpg,.png
本文出自 “灵哥笔记” 博客,请务必保留此出处http://kinghacker.blog.51cto.com/7767090/1677432
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: