完成用户列表Demo,使用plist当数据源,在TableView中显示plist的内容
来源:互联网 发布:ant windows 64位下载 编辑:程序博客网 时间:2024/06/05 04:10
最后设计成果:如上
1.删除Main.storyboard中的ViewController,从右边拖一个TableViewController到storyboard
2.删掉文件中的ViewController.h和ViewController.m,新建class:PlistTableViewController,subclass of UITableViewController,文件列表新增 PlistTableViewController.h,PlistTableViewController.m
3.storyboard中的custom class选择新建的PlistTableViewController
4.从右边拖一个Table View Cell 到Table View,修改属性值的Identifier:userInfoCell
5.选择TableViewController,点击Editor-->Embed In-->Navigation Controller,新增一个导航控制器到storyboard,编辑Title:用户列表
6.设计自定义cell:在storyboard的Table View Cell上从右边拖入你需要的Label,Image View等,拖拽大小布局
7.新建class:MyUserInfoCell,subclass of UITableViewCell,文件列表新增MyUserInfoCell.h,MyUserInfoCell.m
8.在storyboard自定义cell中的custom class选择新建的MyUserInfoCell
9.将MyUserInfoCell中定义的控件属性和storyboard中的空间关联好
10.新建plist文件,信息写入属性列表
11.将需要的图片文件夹复制过来
1.PlistTableViewController.m的关键代码:
#import "PlistTableViewController.h"
#import "MyUserInfoCell.h"
@interface PlistTableViewController ()
{
@private NSMutableArray *_userList;
}
@end
@implementation PlistTableViewController
- (void)viewDidLoad {
[superviewDidLoad];
// 设定pList文件路径
NSString *dataPath = [[NSBundlemainBundle]pathForResource:@"FocusUsers.plist"ofType:nil];
// 填充数组内容
_userList = [NSMutableArrayarrayWithContentsOfFile:dataPath];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_userListcount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"userInfoCell";
MyUserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
// 实例化单元格对象
if (cell == nil) {
cell = [[MyUserInfoCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
}
NSDictionary *item = [_userListobjectAtIndex:indexPath.row];
//显示文本
[cell.userNameLabel1 setText:[item objectForKey:@"JobId"]];
[cell.userNameLabel2 setText:[item objectForKey:@"UserName"]];
[cell.userNameLabel3 setText:[item objectForKey:@"TelNum"]];
[cell.userNameLabel4 setText:[item objectForKey:@"Email"]];
// 显示头像
[cell.userImagesetImage:[UIImageimageNamed:[itemobjectForKey:@"Image"]]];
// 设定字体
[cell.userNameLabel2setFont:[UIFontfontWithName:@"Helvetica"size:16.0f]];
// 改变字体颜色
[cell.userNameLabel2setTextColor:[UIColororangeColor]];
// 调整文本居中对齐
// [cell.textLabel setTextAlignment:NSTextAlignmentCenter];
return cell;
}
@end
2.MyUserInfoCell.h的关键代码:
@interface MyUserInfoCell : UITableViewCell
@property (strong,nonatomic)IBOutletUIImageView *userImage;
@property (strong,nonatomic)IBOutletUILabel *userNameLabel1;
@property (strong,nonatomic)IBOutletUILabel *userNameLabel2;
@property (strong,nonatomic)IBOutletUILabel *userNameLabel3;
@property (strong,nonatomic)IBOutletUILabel *userNameLabel4;
@end
3.MyUserInfoCell.m的关键代码:
#import "MyUserInfoCell.h"
@implementation MyUserInfoCell
@synthesize userImage = _userImage;
@synthesize userNameLabel1 = _userNameLabel1;
@synthesize userNameLabel2 = _userNameLabel2;
@synthesize userNameLabel3 = _userNameLabel3;
@synthesize userNameLabel4 = _userNameLabel4;
- (void)awakeFromNib {
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selectedanimated:animated];
}
@end
<plist version="1.0">
<array>
<dict>
<key>JobId</key>
<string>1</string>
<key>UserName</key>
<string>赵一</string>
<key>TelNum</key>
<string>11111111111</string>
<key>Email</key>
<string>123@qq.com</string>
<key>Image</key>
<string>icon1.jpg</string>
</dict>
<dict>
<key>JobId</key>
<string>2</string>
<key>UserName</key>
<string>钱二</string>
<key>TelNum</key>
<string>22222222222</string>
<key>Email</key>
<string>234@qq.com</string>
<key>Image</key>
<string>icon2.jpg</string>
</dict>
<dict>
<key>JobId</key>
<string>3</string>
<key>UserName</key>
<string>孙三</string>
<key>TelNum</key>
<string>33333333333</string>
<key>Email</key>
<string>345@qq.com</string>
<key>Image</key>
<string>icon3.jpg</string>
</dict>
<dict>
<key>JobId</key>
<string>4</string>
<key>UserName</key>
<string>李四</string>
<key>TelNum</key>
<string>44444444444</string>
<key>Email</key>
<string>456@qq.com</string>
<key>Image</key>
<string>icon4.jpg</string>
</dict>
<dict>
<key>JobId</key>
<string>5</string>
<key>UserName</key>
<string>周五</string>
<key>TelNum</key>
<string>55555555555</string>
<key>Email</key>
<string>567@qq.com</string>
<key>Image</key>
<string>icon5.jpg</string>
</dict>
</array>
</plist>
5.storyboard的界面:
- 完成用户列表Demo,使用plist当数据源,在TableView中显示plist的内容
- iOS项目开发实战——学会使用TableView列表控件(四)plist读取与Section显示
- IOS中plist文件内容的读取
- 如何将iOS工程中的.plist文件里的内容取出在android的工程中使用
- OC文件操作,plist文件的本质,MVC设计模式概念,json本质,tableView数据源
- cocos2d中.plist文件的使用
- plist使用的若干问题
- 【iOS】plist的使用
- plist的使用
- 使用TexturePacker生成png和plist,在Cocos2dx中使用plist和png
- 使用TexturePacker生成png和plist,在Cocos2dx中使用plist和png
- ios中读取plist文件内容的方法
- 在IOS中如何读取系统产生的plist文件和自定义的plist文件
- 在IOS中如何读取系统产生的plist文件和自定义的plist文件
- 在IOS中如何读取系统产生的plist文件和自定义的plist文件
- 使用plist
- iOS-生成plist文件,在项目中代码创建plist
- .plist
- Java杨辉三角的打印
- 正则表达式学习笔记
- Android的OnTouch事件的处理机制
- GTest使用
- js浅copy和深copy
- 完成用户列表Demo,使用plist当数据源,在TableView中显示plist的内容
- 微信营销最重要的环节大部分人都忽略了
- 食物链
- Android 5.0 源码分析 Handler Looper MessageQueue 底层原理
- Django1.9学习笔记
- iOS打包ipa安装包的流程
- SpringMVC入门 -- WebMVC简介
- 51单片机DS1302实时时钟驱动程序
- 理解Python中的装饰器