2016-06-13 99 views
0

有可能是一个更好的方式来做到这一点,但我一直在一整天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”标签,然后当我释放按钮时,所有格式化消失,单元格恢复回来到默认状态。我假设我并没有完全把某个东西挂在某个地方,但任何帮助你追踪我做错的事情,或者建议一个更好的方法来处理这个问题将非常感谢!

原型细胞:

Prototype Cell

负载什么显示:

loaded

什么触摸显示了下来:

loaded

下面截图10

什么显示在触摸起来:

loaded

+0

也许你应该使用部分!所以你会有节标题(现在你有静态单元格),只有一个节会有行(这会动态地改变)。 – tbilopavlovic

回答

0

经过一些更多的实验后,我才弄明白了!回想起来似乎很明显,但是当我实例化我的数据源时,我正在做一些奇怪的循环引用荒谬的东西。我退出了init函数,并引用了我在dataSource子类中使用的tableView参数。这就是我的DataSource类貌似现在:

let dataSource = ValidatedCardsDataSource() 
self.validatedCardsTable.dataSource = dataSource 
self.validatedCardsTable.delegate = dataSource 
class ValidatedCardsDataSource :NSObject, UITableViewDataSource, UITableViewDelegate { 

    var selectedRow = 0 
    let rowCount = 10 

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return 100.0 
    } 

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return 100.0 
    } 


    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 = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell 
     cell.textLabel!.text = "Title of Row: #\(indexPath.row)" 
     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     print(indexPath.row) 
     self.selectedRow = indexPath.row 
     tableView.reloadData() 
    } 
} 

现在我只需要弄清楚我的约束......感谢您的答复!

1

重写你的方法 “的touchesBegan:withEvent:方法/ touchesEnded:withEvent:方法/ touchesCancelled:withEvent:方法” 当电池降落/ upinside/upoutside,改变UI样式。