Hitesh Surani Hitesh Surani - 4 months ago 11
Swift Question

Two UITableView in single UIViewcontroller not working properly

I want to implement following functionality in app show pict

enter image description here

But i have following problem show another pict

enter image description here

my code as follow

// MARK: UITextFieldDelegate Methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if str == "Loading"{
return 0
}else if tableView == tbl2{
return arrSub.count
}else{
return self.displayData.count
}
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:customCellInvitation = self.tableView.dequeueReusableCellWithIdentifier("cell")as! customCellInvitation
if tableView == tbl2{
//Code for the load secind table
cell.lblUserName.text = self.arrSub.objectAtIndex(indexPath.row).valueForKey("username") as?String
cell.btnAdd.setImage(UIImage(named: "yes1.png"), forState:(UIControlState.Normal))
return cell
}else{
//Code for the load first table
cell.lblUserName.text = self.displayData.objectAtIndex(indexPath.row).valueForKey("username") as?String
cell.btnAdd.setImage(UIImage(named: "add.png"), forState:(UIControlState.Normal))
cell.btnAdd.setImage(UIImage(named: "yes1.png"), forState:(UIControlState.Selected))
cell.btnAdd.addTarget(self, action: "addData:", forControlEvents: .TouchUpInside)
cell.btnAdd.tag = indexPath.row
}
return cell
}


// MARK: UITableViewDelegate Methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 44
}
//function call when user click Plus button
func addData(sender: UIButton!) {
arrSub .addObject(self.displayData .objectAtIndex(sender.tag))
var button:UIButton = sender.viewWithTag(sender.tag) as! UIButton
button.selected=true
button.userInteractionEnabled = false
NSLog("%@", arrSub)

[tbl2 .reloadData()]
}

Answer
    // MARK:  UITextFieldDelegate Methods

      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            var cell:customCellInvitation = self.tableView.dequeueReusableCellWithIdentifier("cell")as! customCellInvitation
       if tableView == tbl2{
                cell.lblUserName.text = self.arrSub.objectAtIndex(indexPath.row).valueForKey("username") as?String
          if str = "yes"{ 
                   cell.btnAdd.setImage(UIImage(named: "yes1.png"), forState:(UIControlState.Normal))

          }else{
                   cell.btnAdd.setImage(UIImage(named: "NO.png"), forState:(UIControlState.Normal))
          }
      return cell
       }else{
         cell.lblUserName.text = self.displayData.objectAtIndex(indexPath.row).valueForKey("username") as?String
             if str = "yes"{ 
               cell.btnAdd.setImage(UIImage(named: "yes1.png"), forState:(UIControlState.Normal))
      }else{
         cell.btnAdd.setImage(UIImage(named: "NO.png"), forState:(UIControlState.Normal))
      }
    }
    return cell
  }
Comments