本篇文章主要介绍了"IOS中使用UITableView加载数据项",主要涉及到方面的内容,对于IOS开发感兴趣的同学可以参考一下:
ios中用UITableView加载数据 实现步骤 1:添加数据源和数据代理模式 2:设置数据源和代理 3:实现 具体步骤如下: 1:添加数据源...
ios中用UITableView加载数据
实现步骤
1:添加数据源和数据代理模式
2:设置数据源和代理
3:实现
具体步骤如下:
1:添加数据源和数据代理模式
在oc中添加的方法如下图1位置标注

在@interface viewcontrol()后面用“<>”写上UITableViewDataSource和UITableViewDelegate.
第二步:
在程序加载时设置table的数据源和数据代理模式代码如下:
self.uitableViewshow.dataSource =
self;
self.uitableViewshow.delegate =
self;
第三步:实现如下
3.1:这里先介绍一下表格的最常用的几个方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
}
这一部分代表该表格有几个section,通常我们不去设置该属性,默认为1.
3.2 设置一个section显示的多少行数据,返回的是一个nsinter类型(此方法实现必不可少)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;
}
3.3 设置每行显示的内容(此方法必不可少)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCellStyleSubtitle 这个文件可以显示detialTextLabel.tex
//UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"w"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"w"];
}
MjHero *hers = self.heros[indexPath.row];
cell.textLabel.text = hers.name;
cell.detailTextLabel.text = hers.intro;
cell.imageView.image = [UIImage imageNamed:hers.icon];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//添加控件
//cell.accessoryView = [[UISwitch alloc] init];
//设置指示器的
return cell;
}
//这里解释一下作用。
这里返回的是一个uitableviewcell,在ios中我们可以利用 dequeueReusableCellWithIdentifier来创建一个可重复利用的cell。因为table在每次读取的时候会读取手机屏幕显示的数量,而不是每次把数据源里的数量一次性读完。苹果公司在这里做了很好的优化。
首先用dequeueReusableCellWithIdentifier寻找可重复利用的cell
如果没找到 则创建一个可重复利用的cell,用alloc开辟创建。
cell.accessoryType =
UITableViewCellAccessoryDisclosureIndicator;这里可以设置每一个cell显示的样式。具体的样式大家可以试一试。当然还有一些cell的属性了。
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了IOS中使用UITableView加载数据项,包括了方面的内容,希望对IOS开发有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_146062.html
相关图片
相关文章