有可能是一个更好的方式来做到这一点,但我一直在一整天google搜索,并不能找到我的具体问题的解决方案:动态UITableView的数据源静态的UITableView细胞内
我有一个静态的TableView这显示相当数量的信息,并包含一个文本输入部分,用于将值添加到嵌套在父级静态TableView的一个单元格中的动态TableView中。
为此,我的UITableViewDataSource和UITableViewDelegate的子类在我的UITableViewController中,它将控制嵌套的TableView中显示的内容。我有一个出口,以嵌套表:
@IBOutlet weak var validatedCardsTable: UITableView!
在我viewDidLoad
有:
let dataSource = ValidatedCardsDataSource(tableView: self.validatedCardsTable)
self.validatedCardsTable.dataSource = dataSource
self.validatedCardsTable.delegate = dataSource
和我的子类看起来是这样的:
class ValidatedCardsDataSource :NSObject, UITableViewDataSource, UITableViewDelegate {
var tableView:UITableView
init(tableView : UITableView) {
self.tableView = tableView
super.init()
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 1
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = "Title of Row: #\(indexPath.row)"
return cell
}
}
当我运行这段代码,嵌套TableView加载我在cellForRowAtIndexPath
中设置的适当的“行的标题”文本(虽然tableview没有调整大小,但我确定完全是另一个问题)。然而,当我在任何地方按下TableView时,我在故事板中的单元格原型中指定了“A Label”标签,然后当我释放按钮时,所有格式化消失,单元格恢复回来到默认状态。我假设我并没有完全把某个东西挂在某个地方,但任何帮助你追踪我做错的事情,或者建议一个更好的方法来处理这个问题将非常感谢!
原型细胞:
负载什么显示:
什么触摸显示了下来:
下面截图10什么显示在触摸起来:
也许你应该使用部分!所以你会有节标题(现在你有静态单元格),只有一个节会有行(这会动态地改变)。 – tbilopavlovic