【问题标题】:TableView scrolls automatically while selecting a check box选中复选框时 TableView 自动滚动
【发布时间】:2016-11-10 19:51:56
【问题描述】:

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

【问题讨论】:

    标签: ios swift tableview


    【解决方案1】:

    它可能是自动滚动的,因为您的 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

    【讨论】:

    • 谢谢 Tommy,但我正在使用 swift。你能帮忙吗?
    • API 和swift中的语法一样
    【解决方案2】:

    正如您所说,当您单击复选框表时会滚动,所以问题可能在 tickClicked 中。

    【讨论】:

    • 嗨 Bharat,我在其他屏幕上做同样的事情没有任何问题,但我在使用两个自定义单元格时遇到问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    相关资源
    最近更新 更多