在 Table 中的数组中添加部分 在 iOS 中查看
Add Sections in an Array in Table View in iOS
我正在调用一个 API,获取一个数组作为响应,然后在我的 table 视图中填充该数组。现在我希望如果数组有 2 个或更多元素,那么每个元素都应该显示在 table 视图的单个部分下。部分也会有一些标题。我如何使用现有数组执行此操作?例如,我收到 array["A","B"]
。现在,当 table 视图应该加载时,它应该在 section1 下显示元素 "A",并且元素 "B" 应该在 section2 下显示。我从响应中获得的数组怎么会发生这种情况?
为了每个数组元素都有一个部分,return array.count
in func numberOfSections(in tableView: UITableView)
在func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int)
中设置return 1
在 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
中将单元格值指定为 array[indexPath.section]
如果您想为每个元素显示一个部分,您应该 return numberOfSection
中的总数组计数。我还在 header.
部分添加了用于添加标题的代码
func numberOfSections(in tableView: UITableView) -> Int {
return myArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.lightGray
let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width:
tableView.bounds.size.width, height: tableView.bounds.size.height))
headerLabel.font = UIFont(name: "Verdana", size: 20)
headerLabel.textColor = UIColor.white
headerLabel.text = "Section: \(section+1)"
headerLabel.sizeToFit()
headerView.addSubview(headerLabel)
return headerView
}
最初采用 1 个元素的数组,最初执行 array.append("");
,然后在 numberOfSections(in tableView: UITableView) -> Int
函数中执行 return array.count
。
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return array.count
}
将您的 api 数据读取到同一个数组对象中 & 一旦您的 API 调用完成,您可以使用 tableView.reloadData()
重新加载 tableView 数据
在 titleForHeaderInSection section
中使用这个数组
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return array[section]
}
我正在调用一个 API,获取一个数组作为响应,然后在我的 table 视图中填充该数组。现在我希望如果数组有 2 个或更多元素,那么每个元素都应该显示在 table 视图的单个部分下。部分也会有一些标题。我如何使用现有数组执行此操作?例如,我收到 array["A","B"]
。现在,当 table 视图应该加载时,它应该在 section1 下显示元素 "A",并且元素 "B" 应该在 section2 下显示。我从响应中获得的数组怎么会发生这种情况?
为了每个数组元素都有一个部分,
return array.count
infunc numberOfSections(in tableView: UITableView)
在
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
中设置在
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
中将单元格值指定为
return 1
array[indexPath.section]
如果您想为每个元素显示一个部分,您应该 return numberOfSection
中的总数组计数。我还在 header.
func numberOfSections(in tableView: UITableView) -> Int {
return myArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.lightGray
let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width:
tableView.bounds.size.width, height: tableView.bounds.size.height))
headerLabel.font = UIFont(name: "Verdana", size: 20)
headerLabel.textColor = UIColor.white
headerLabel.text = "Section: \(section+1)"
headerLabel.sizeToFit()
headerView.addSubview(headerLabel)
return headerView
}
最初采用 1 个元素的数组,最初执行 array.append("");
,然后在 numberOfSections(in tableView: UITableView) -> Int
函数中执行 return array.count
。
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return array.count
}
将您的 api 数据读取到同一个数组对象中 & 一旦您的 API 调用完成,您可以使用 tableView.reloadData()
重新加载 tableView 数据
在 titleForHeaderInSection section
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return array[section]
}