首先来看下效果图:
在这个图中可以看出我们没有tableview的分割线,而且每个单元格都是可以设置自己的样式。
下面开始来学习字母制作自己的自定义的样式的单元格。

1.先建立一个<code>CustomViewController</code>样式的xib和对应的swift文件,如下图所示:
一定要记住勾选 <code>Also create XIB file</code> 这样就可以建立一个与之对应的xib文件

2. 在<code>CustomViewController.xib</code>文件中加入<code>tableView</code>的控件,然后设置对应的与<code>CustomViewController.swift</code>相对应的联系
使得tableview的<code>delegate</code>和<code>datasource</code>都与界面<code>CustonViewController</code>联系起来,如下图所示:

也可在<code>CustonViewController.swift</code>中直接继承这俩个类<code>UITableViewDelegate</code>和<code>UITableViewDatasource</code>
代码段为:
<code>
class CustomViewController: UIViewController,UITableViewDelegate,UITableViewDataSource
</code>
此时<code>CustonViewController.swift</code>基本完成。
2. 在建立一个tableviewCell样式的xib和对应的swift文件,如下图所示:

3.建立完cell 文件之后我们就可以在<code>CustomTableViewcell.xib</code>文件中来进行编写自定义的单元格了。
首先设置contentview的颜色background,如图所示:

在设置tableviewcell的identifier 的为 CustomTableViewCell

其次在该conentview 中加入一个view,但是该view不铺满整个view,然后在view中添加自己自定义的控件,我的图层建立如下图所示;


然后就是重点了,建立各个控件对应的代码联系,如图所示:
注意,前面的四个可以不写对应的,但是<code>uiview</code>的一定要写,这样之后的圆角边框就可以出现了,下面来用代码来实现控件的样式。初始化函数awakeFromNib()中写入以下代码:
<code>
//设置头像是圆形显示
customImage.layer.masksToBounds = true
customImage.layer.cornerRadius = customImage.frame.size.width/2
//设置cell是有圆角边框显示
customView.layer.masksToBounds = true
customView.layer.cornerRadius = 10
</code>
这样我们的cell 文件就已经设置完毕,下面我们就可以开始来设置<code>CustonViewController.swift</code>的文件了。
4.设置<code>CustonViewController.swift</code>文件
现在初始化文件<code>overridefuncviewDidLoad()</code>中加入加载cell代码:
<code>
//加载CustomTableViewCell
self.tableView.registerNib(UINib(nibName:"CustomTableViewCell", bundle:nil), forCellReuseIdentifier:"CustomTableViewCell")
</code>
然后开始将cell加载到tableview 中,其代码如下图所示:
<code>
// MARK: - Table view data source
//设置不同种类的单元格的样式有多少
overridefuncnumberOfSectionsInTableView(tableView:UITableView) ->Int{
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return1
}
//设置所需要的单元格的个数
overridefunctableView(tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
// #warning Incomplete method implementation.
// Return the number of rows in the section.
returncellLabel.count
}
//在单元格中加载cell
overridefunctableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) ->UITableViewCell{
letcell:CustomTableViewCell= tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell")as!CustomTableViewCell
cell.customLabel.text=cellLabel[indexPath.row]
cell.customImage.image=UIImage(named:"images.png")
returncell
}
//设置单元格的大小
overridefunctableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath) ->CGFloat{
return120.0
}
</code>
在启动文件<code>AppDelegate.swift</code> 文件中的<code>application</code>函数中加入该语句来启动<code>CustonViewController.swift</code>
<code>
self.window?.rootViewController=CustomTableViewController()
</code>
启动之后我们可以见到如下界面

我们可以看到viewtable的线条还存在,而且下面的显示还是白色的,下面来设置使他们消失。
将tableview中<code>separtor</code>设置为<code>none</code>,这样表格中的线条就消失了

将view中的<code>background</code>的颜色设置与<code>cell</code>中的背景颜色一样即可。

然后在运行 ,结果如下:

详细代码见百度云直接下载:http://pan.baidu.com/s/1mgmtYVQ
网友评论
在cell.titleName.text = items[indexPath.row]这句报以上错误是什么情况