【发布时间】:2019-11-29 04:28:54
【问题描述】:
我基本上是在尝试制作一个可扩展的 UITableview 我有两个不同的数据模型(添加帖子)和(reviewPosts)我创建了一个结构来容纳这两个模型,然后我使用一个计算属性来返回我这两个模型以便我可以喂我的 tableview 数组。我已将所有逻辑设置为隐藏和显示部分,但是在设置变量 isExpanded 时我不能,因为我的计算变量是只读的。我不确定如何设置 newValue (bool)。
// class for constructing tableview data
class FeedConstructor {
public static var feedPosts: [ExpandableItem] {
var isOpen = true
return [ExpandableItem(isExpanded: isOpen, items: posts),
ExpandableItem(isExpanded: isOpen, items: reviews)]
}
static var posts = [AddPost]()
static var reviews = [ReviewPost]()
}
// here I am trying to set the isExpanded property
class ViewController: UIViewController {
func dropDownPressed(sender: UITapGestureRecognizer) {
guard let section = sender.view?.tag else {return}
var indexPaths = [IndexPath]()
for row in FeedConstructor.feedPosts[section].items.indices{
print(section,row)
let indexPath = IndexPath(row: row, section: section)
indexPaths.append(indexPath)
}
// I cant set this property because its get only..
let isExpanded = FeedConstructor.feedPosts[section].isExpanded
FeedConstructor.feedPosts[section].isExpanded = !isExpanded!
if isExpanded! {
tableView.deleteRows(at: indexPaths, with: .fade)
} else {
tableView.insertRows(at: indexPaths, with: .fade)
}
}
}
需要能够在每次选择tableview header 时设置isExpanded 属性。但是我不确定如何使用 setter 来做到这一点..
【问题讨论】:
标签: arrays swift computed-properties