在iOS开发中,tableView 是一种比较常用的控件, 顾名思义,tableView,即表格视图,用于展示一些具有一定规范的图片或者文字等。
要使用,首先需要创建一个tableView的对象
由于在一个界面中,tableView往往会在几个不同的地方被使用,所以,我们一般把tableView设置为一个全局变量或者是一个属性,这里我们在一个viewController.h文件中创建了一个tableView的全局变量。
<pre name="code" class="objc"><span style="font-size:14px;"> UITableView * _tableView;</span>
与许多对象一样,tableView在使用之前需要对其进行初始化。
与大多数iOS控件一样,tableView拥有initWithFrame方法。
<p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; color: rgb(61, 29, 129);"><pre name="code" class="objc"><span style="font-size:14px;">_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];</span>
此外,tableView还拥有另一个初始化方法
<pre name="code" class="objc"><span style="font-size:14px;">_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 480) style:UITableViewStylePlain];</span>
在第二个方法中,多出了一个style属性,该属性有两个值可选
UITableViewStylePlain和
UITableViewStyleGrouped
对应普通风格和分组风格,
Plain风格如下左所示,即普通的表格形式,数据会依次排列而如右图所示Grouped的风格则是会对表格中的数据进行分组。
两种风格的表格在对应的需求下都有可能会用到(打开你的通讯录,里面的名字就是按照字母分组的表格)。
不同于其他一些基本控件(Lable,button等),我们对tableView所做的操作一般都不是直接操作tableView的对象,而是需要通过它的代理方法来进行。为此,我们要将我们的tableView的代理设置为当前的viewController
<span style="font-size:14px;"> _tableView.delegate = self;
_tableView.dataSource = self;</span>
直接写这两行会有警告
原因是当前的viewController没有遵守tableView的代理,需要在viewController.h文件中遵守代理,我们会需要向tableView中添加数据等操作,所以需要导入两个代理:
<p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo; color: rgb(97, 34, 174);"><pre name="code" class="objc"><span style="font-size:14px;">@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate></span>
此时,原有的两个警告消失了, 但是又会有新的两个警告出现在viewController.m文件中
点击进入UITableViewDataSource中,可以看到
<pre name="code" class="objc"><span style="font-size:14px;">@protocol UITableViewDataSource<NSObject>
@required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;</span>
上面两个就是警告中提到的两个方法,由于它们被@required所修饰,所以我们必须要在viewController.m中执行这两个方法
<span style="font-size:14px;">-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
}
cell.textLabel.text = _dataArray[indexPath.row];
return cell;
}</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:14px;">以上是向一个viewController.中添加一个tableView的步骤,关于如何具体使用,将在之后的文章中写到。</span></span>