我有3个数组,即todayAry、thisWeekAry、upcomingAry和我想要根据所有三个数组中的数据创建节,比如如果所有数组计数都大于0,那么就创建3个节;如果计数2,那么两个部分;如果数组中没有数据,那么在tableView委托方法中创建它的最佳条件是什么?提供此代码的任何替代方案!!提前谢谢!!编码愉快!!
目前我的代码是:
func numberOfSections(in tableView: UITableView) -> Int {
if self.todayDataAry.count > 0 {
if self.thisWeekDataAry.count > 0 {
if self.upcomingDataAry.count > 0 {
return 3 // all sections today,this week,upcoming
} else {
return 2 //today,this week
}
} else {
if self.upcomingDataAry.count > 0 {
return 2 //today,upcoming
} else {
return 1 //today
}
}
} else {
if self.thisWeekDataAry.count > 0 {
if self.upcomingDataAry.count > 0 {
return 2 //this week, upcoming
} else {
return 1 //this week
}
} else {
if self.upcomingDataAry.count > 0 {
return 1 //upcoming
} else {
return 0 //no data
}
}
}
}发布于 2018-02-08 14:40:44
创建另一个数组并在其中存储3个数组
var allDetails = [todayAry, thisWeekAry, upcomingAry]
var filteredDetails = allDetails.filter { !$0.isEmpty }那就像这样改变。过滤器移除空数组并返回非空数组计数。
func numberOfSections(in tableView: UITableView) -> Int {
return filteredDetails.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return filteredDetails[section].count
}您也可以在cellForRow方法中这样做。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil
{
cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "tripHistory")
}
cell.textLabel.text = filteredDetails[indexPath.section][indexPath.row]
}https://stackoverflow.com/questions/48687998
复制相似问题