1 swi_crecell
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5;
}
// cell高度
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44;
}
// header 高度
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0;
}
// footer 高度
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0;
}
// cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.cellForRow(at: indexPath);
if (cell == nil) {
cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cellID") ;
}
cell?.textLabel?.text = "123";
return cell!;
}
// header view
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return UIView.init();
}
// footer view
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView.init();
}
2 代码体
swi_codebody
// MARK:0 设置UI
// MARK:1 数据加载
// MARK:2 事件响应
// MARK:3 懒加载
// MARK:4 方法抽取
3 创建一个按钮
let btn = UIButton.init();
btn.setTitle("", for: UIControl.State.normal);
btn.setTitleColor(UIColor.black, for: UIControl.State.normal);
btn.backgroundColor = UIColor.white;
btn.titleLabel?.font = UIFont.systemFont(ofSize: 15);
btn.addTarget(self, action: #selector(btnClick), for: UIControl.Event.touchUpInside);
self.view.addSubview(btn)
4 创建CollectionView
// sections count
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 3;
}
// rows count
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5;
}
// itemSize
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 0, height: 0);
}
// header Size
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: 0, height: 0);
}
// footer Size
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: 0, height: 0);
}
// minimumLineSpacing 最小行间距
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0;
}
// minimumInteritemSpacing
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0;
}
// insetForSection
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0);
}
// header 或者 footer
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionHeader
{
let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "UICollectionReusableView", for: indexPath);
view.frame = CGRect(x: 0, y: 0, width: self.collectionView.frame.width, height: 40);
let lbl = UILabel.init(frame: CGRect(x: 10, y: 0, width: 150, height: 40));
lbl.textColor = UIColor.init(red: 152/255, green: 152/255, blue: 152/255, alpha: 1);
lbl.font = UIFont.systemFont(ofSize: 13);
lbl.text = "输入你的标题";
view.addSubview(lbl);
return view;
}
return UICollectionReusableView();
}
// collection Cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "", for: indexPath) as! UICollectionViewCell;
return cell;
}
// didselected
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
Swift UITableView & UICollectionView 实现
本文详细介绍使用Swift语言实现UITableView和UICollectionView的方法,包括设置单元格数量、高度、填充视图及响应选择事件等核心功能。代码示例清晰,适合Swift初学者及iOS应用开发者参考。
1006

被折叠的 条评论
为什么被折叠?



