David Robertson David Robertson - 3 months ago 12
Swift Question

Appending tableview custom cells from two different nibs Swift

I have an

uitableview
with two custom-cell xib files (one has
uilabel
, second one
uibutton
).

What i do -

1. i append tableview with three cells (with
uilabel
- first xib)

2. then i load the fourth cell with (with
uibutton
- second xib)

uilabel
text is fetched from the array,
uibutton currentTitle
is also fetched, but from another array.

My goal is - upon the click of the
uibutton
it should append 1. next array of cells with
uilabel
and 2. 1 cell with
uibutton
from the corresponding array.

Here is my code for
tableview


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return linesmain["start"]!.count + 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

if(indexPath.row < someTagsArray.count){
var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell

cell.lblCarName.text = linesmain["start"]![indexPath.row]

return cell

} else {
var celle:vwAnswers = self.tableView.dequeueReusableCellWithIdentifier("cell2") as! vwAnswers
celle.Answer1.setTitle(answersmain["start"]![0], forState:UIControlState.Normal)
return celle
}}


I know I should use something like
InsertRowsAtIndexPath
, but i have no idea how to implement it correctly in my case.

Answer

try

  tableView.beginUpdates()
     var indexPath = NSIndexPath(forRow:row,inSection:0)
     var indexPath2 = NSIndexPath(forRow:row,inSection:0)
     self.tableView?.insertRowsAtIndexPaths([indexPath,indexPath2], withRowAnimation: UITableViewRowAnimation.Left)
tableView.endUpdates()

Reference Apple swift sample : https://developer.apple.com/library/ios/samplecode/Lister/Listings/Swift_ListerToday_TodayViewController_swift.html