0

I have created two custom cells in tableview and on the second cell, I have a list of check box items. When I click on check box the tableview scrolls automatically.

Here is my code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == 0
    {
        let cell:SharedFirstTableViewCell = self.ListTableview.dequeueReusableCellWithIdentifier("CellF")! as! SharedFirstTableViewCell
        tableView.estimatedRowHeight = 364.0
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView .separatorColor = UIColor .clearColor()
        tableView .tableFooterView = UIView (frame:CGRectZero)
        cell .selectionStyle = UITableViewCellSelectionStyle .None
        cell.contentView .addSubview(collectionView)
        cell.btn_SelectAll.tag=indexPath.row
        cell.btn_SelectAll.addTarget(self, action:#selector(ShareViewController.SelectAll(_:)), forControlEvents: .TouchUpInside)
        cell.btn_ByCountry.tag=indexPath.row
        cell.btn_ByCountry.addTarget(self, action:#selector(ShareViewController.Country(_:)), forControlEvents: .TouchUpInside)
        groupname = cell.txt_GroupName
        if country.isEmpty == true {
            cell.lbl_ByCountry.text = "By Country"
        }
        else
        {
            cell.lbl_ByCountry.text = country
        }
        if load == true
        {
            cell.btn_SelectAll.setImage((UIImage (named: "box.png")), forState: .Normal)
        }
        else
        {

            cell.btn_SelectAll.setImage((UIImage (named: "Full Image-30.png")), forState: .Normal)
        }

        return cell
    }

    else
    {
        let cellFirst:SharedTwoTableViewCell = self.ListTableview.dequeueReusableCellWithIdentifier("CellT")! as! SharedTwoTableViewCell

        ListTableview.bounces = false
        tableView.estimatedRowHeight = 57.0
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView .separatorColor = UIColor .clearColor()
        tableView .tableFooterView = UIView (frame:CGRectZero)


        cellFirst.lbl_Name.text = newDictionary .objectAtIndex(indexPath.row).valueForKey("name") as? String
        cellFirst.lbl_Address.text = newDictionary .objectAtIndex(indexPath.row).valueForKey("address") as? String
        let url_image  =  newDictionary .objectAtIndex(indexPath.row).valueForKey("profile_pic") as? String

        if url_image != nil {

            imageURL = ""
            imageURL  = imageURLs+(url_image )!

            cellFirst.img_profile.hnk_setImageFromURL(NSURL(string: imageURL)!)


        }

        cellFirst.tickButton.tag=indexPath.row
        cellFirst.tickButton.addTarget(self, action:#selector(ShareViewController.tickClicked(_:)), forControlEvents: .TouchUpInside)


        if selectedArray .containsObject(newDictionary.objectAtIndex(indexPath.row))
        {
            cellFirst.tickButton.setImage((UIImage (named: "box.png")), forState: .Normal)
        }
        else
        {

            cellFirst.tickButton.setImage((UIImage (named: "Full Image-30.png")), forState: .Normal)
        }

        return cellFirst
    }
}
Danh
  • 5,916
  • 7
  • 30
  • 45

2 Answers2

0

It's probably auto scrolling because your tickClicked handler is making a textfield a first responder.

You can disable this by disabling the table view's scrolling during this transition:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    // This is solving the issue where making the text field first responder
    // automatically scrolls the scrollview down by the height of the search bar.
    if (!scrollView.isDragging && !scrollView.isDecelerating &&
        self.searchField.isFirstResponder &&
        (scrollView.contentOffset.y < -scrollView.contentInset.top)) {

        [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, -scrollView.contentInset.top) animated:NO];
    }
}

Other methods can be found here:

Disable UIScrollView scrolling when UITextField becomes first responder

Disabling automatic scrolling of UITableView when editing UITextField inside UITableViewCell

Community
  • 1
  • 1
tommybananas
  • 5,718
  • 1
  • 28
  • 48
0

As you said when you click on check box table going to scroll so problem maybe in tickClicked.

  • Hi Bharat, i am doing the same thing in other screen without any problem but am facing problem where i am using two custom cells. – deepak vaishnav Nov 10 '16 at 20:24