iOS:如何在不使用第三方和 MVC 模式的情况下在 Swift 中创建可扩展的 TableView? [英] iOS: How to create Expandable TableView in Swift without using Third parties and MVC Pattern?
本文介绍了iOS:如何在不使用第三方和 MVC 模式的情况下在 Swift 中创建可扩展的 TableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在 swift 中实现了 Tableview,但我想制作可扩展的 TableView 请给出想法..
这是Tableview的代码..
//MARK: - TableView 委托和数据源func tableView(_ tableView: UITableView, numberOfRowsInSection 部分: Int) ->整数{返回 MenuNameArray.count}func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell", for: indexPath) as!菜单表视图单元格cell.menuNameLabel.text = NameArray[indexPath.row]返回单元格}func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}
解决方案
Here You Go , Try This Out :-
struct ExpandableNames {var isExpanded : Bool变量名称:[字符串]}结构联系{让名称:字符串}
<块引用>
在你的班级 - >
var twoDArray = [ExpandableNames(isExpanded : true,names:["Krishna","Rishabh","Aditya","Chandan","Nipun","Navdeesh","Steve"].map{联系人(姓名:$0)}),ExpandableNames(isExpanded : true,names:["Carl","Michal","Tommy","Jennny","Vikram","Swati"].map{联系人(姓名:$0)}),ExpandableNames(isExpanded : true,names:["David","dude","dfff","dcc","daa","dee","dsss"].map{联系人(姓名:$0)}),ExpandableNames(isExpanded : true,names:[Contact(names: "Pattrick", hasFav: false)])]让 cellId = "cellID"让标识符 = "attachmentCellID"func tableView(_ tableView: UITableView, heightForHeaderInSection 部分: Int) ->CGFloat{返回 50}func numberOfSections(in tableView: UITableView) ->整数{返回 twoDArray.count}func tableView(_ tableView: UITableView, numberOfRowsInSection 部分: Int) ->整数{如果 !twoDArray[section].isExpanded{返回 0}返回 twoDArray[section].names.count}func tableView(_ tableView: UITableView, viewForHeaderInSection 部分: Int) ->界面视图?{让 btn = UIButton(类型:.system)如果(部分== 0){btn.setTitle("点击查看类", for: .normal)}否则如果(部分== 1){btn.setTitle("Tap To View Admins", for: .normal)}btn.setTitleColor(.black, for: .normal)btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)btn.addTarget(self, action: #selector(handleExpandClose), for: .touchUpInside)btn.backgroundColor = AppColors.greyBorderColorbtn.tag = 部分返回 btn}@objc func handleExpandClose(button : UIButton){让部分 = button.tagvar indexPaths = [IndexPath]()对于 twoDArray[section].names.indices 中的行{让 indexPath = IndexPath(row: row, section: section)indexPaths.append(indexPath)}让 isExpanded = twoDArray[section].isExpandedtwoDArray[section].isExpanded = !isExpandedbutton.setTitle(isExpanded ? "Tap To View Classes" : "Classes", for: .normal)如果是扩展{tableView.deleteRows(at: indexPaths, with: .fade)}别的{tableView.insertRows(at: indexPaths, with: .fade)}}
<块引用>
并在 cellForRowAt 中休息 -
let contact = twoDArray[indexPath.section].names[indexPath.row]cell.textLabel?.text = contact.names
I have Implemented Tableview in swift, but I want to Make Expandable TableView Please Give the idea..
This the code for Tableview..
//MARK: - TableView Delegate and Datasource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return MenuNameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell", for: indexPath) as! MenuTableViewCell
cell.menuNameLabel.text = NameArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
解决方案
Here You Go , Try This Out : -
struct ExpandableNames {
var isExpanded : Bool
var names : [String]
}
struct Contact {
let names : String
}
in your Class - >
var twoDArray = [
ExpandableNames(isExpanded : true,names:["Krishna","Rishabh","Aditya","Chandan","Nipun","Navdeesh","Steve"].map
{
Contact(names: $0)
}),
ExpandableNames(isExpanded : true,names:["Carl","Michal","Tommy","Jennny","Vikram","Swati"].map
{
Contact(names: $0)
}),
ExpandableNames(isExpanded : true,names:["David","dude","dfff","dcc","daa","dee","dsss"].map
{
Contact(names: $0)
}),
ExpandableNames(isExpanded : true,names:[Contact(names: "Pattrick", hasFav: false)])
]
let cellId = "cellID"
let identifier = "attachmentCellID"
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
return 50
}
func numberOfSections(in tableView: UITableView) -> Int
{
return twoDArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if !twoDArray[section].isExpanded
{
return 0
}
return twoDArray[section].names.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let btn = UIButton(type: .system)
if(section == 0)
{
btn.setTitle("Tap To View Classes", for: .normal)
}
else if(section == 1)
{
btn.setTitle("Tap To View Admins", for: .normal)
}
btn.setTitleColor(.black, for: .normal)
btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
btn.addTarget(self, action: #selector(handleExpandClose), for: .touchUpInside)
btn.backgroundColor = AppColors.greyBorderColor
btn.tag = section
return btn
}
@objc func handleExpandClose(button : UIButton)
{
let section = button.tag
var indexPaths = [IndexPath]()
for row in twoDArray[section].names.indices
{
let indexPath = IndexPath(row: row, section: section)
indexPaths.append(indexPath)
}
let isExpanded = twoDArray[section].isExpanded
twoDArray[section].isExpanded = !isExpanded
button.setTitle(isExpanded ? "Tap To View Classes" : "Classes", for: .normal)
if isExpanded
{
tableView.deleteRows(at: indexPaths, with: .fade)
}
else
{
tableView.insertRows(at: indexPaths, with: .fade)
}
}
And Rest in cellForRowAt -
let contact = twoDArray[indexPath.section].names[indexPath.row]
cell.textLabel?.text = contact.names
这篇关于iOS:如何在不使用第三方和 MVC 模式的情况下在 Swift 中创建可扩展的 TableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文