2016-11-10 60 views
0

我在tableview和第二個單元格中創建了兩個自定義單元格,我有一個複選框項目列表。當我點擊複選框時,tableview會自動滾動。TableView在選擇複選框時自動滾動

這裏是我的代碼:

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 
    } 
} 

回答

0

這也可能是自動滾動,因爲你的tickClicked處理程序製作文本框第一個響應。

您可以在此過渡期間禁用表視圖的滾動禁用此:

- (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]; 
    } 
} 

其他方法可以在這裏找到:

Disable UIScrollView scrolling when UITextField becomes first responder

Disabling automatic scrolling of UITableView when editing UITextField inside UITableViewCell

+0

謝謝湯米,但我使用迅速。你能幫忙嗎? –

+0

該API是相同的語法只是在迅速 – tommybananas

0

至於你說的時候你點擊複選框表將滾動,所以問題可能在tickClicked。

+0

嗨Bharat,我在其他屏幕做同樣的事情沒有任何問題,但面臨的問題,我使用兩個自定義單元格。 –