Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用Swift在半横幅视图上创建UITableview第一个索引单元格显示?_Ios_Swift_Uitableview_Storyboard - Fatal编程技术网

Ios 如何使用Swift在半横幅视图上创建UITableview第一个索引单元格显示?

Ios 如何使用Swift在半横幅视图上创建UITableview第一个索引单元格显示?,ios,swift,uitableview,storyboard,Ios,Swift,Uitableview,Storyboard,在我的场景中,我尝试创建带有卡效果和顶部横幅的UITableview。我已经添加了它将像可伸缩表格视图标题一样工作。我所做的一切都是使用故事板完成的,但我一直在显示tableview的一半,第一个索引单元格显示在横幅头部。我尝试了许多想法,但没有完成。为实现此设计提供一些想法 电流输出: 预期产出: 从您的演示项目中,我对您现有的代码做了一些更改,例如您的scrollViewDidScroll将: var lastContentOffset: CGFloat = 0 func scrollV

在我的场景中,我尝试创建带有
卡效果和顶部横幅的
UITableview
。我已经添加了它将像
可伸缩
表格视图
标题
一样工作。我所做的一切都是使用
故事板
完成的,但我一直在显示
tableview的
一半
第一个
索引单元格
显示在
横幅
头部。我尝试了许多想法,但没有完成。为实现此
设计提供一些想法

电流输出:

预期产出:

从您的演示项目中,我对您现有的代码做了一些更改,例如您的
scrollViewDidScroll
将:

var lastContentOffset: CGFloat = 0

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    self.lastContentOffset = scrollView.contentOffset.y
}


func scrollViewDidScroll(_ scrollView: UIScrollView) {

    if self.lastContentOffset < scrollView.contentOffset.y {
        //Up
        let y = 220 - (scrollView.contentOffset.y + 220)
        let height = min(max(y, 60), 220)
        if height >= 128 {
            imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height + 70)
        }
    } else if self.lastContentOffset > scrollView.contentOffset.y {
        //Down
        let y = 300 - (scrollView.contentOffset.y + 300)
        let height = min(max(y, 60), 400)
        imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height + 80)
    }
}
结果将是:


是更新项目的链接。

您可以使用tableview
contentInset
contentOffSet
来调整您的位置。可以尝试让标题clipToBounds=false显示在其框架外。我尝试过,但不起作用@karthikAwesome help。谢谢你,伙计。iOS Master@Dharmesh Khenish很高兴帮助您……)
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    let imageView = UIImageView()
    var lastContentOffset: CGFloat = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.estimatedRowHeight = 50
        tableView.contentInset = UIEdgeInsetsMake(220, 0, 0, 0)
        tableView.backgroundColor = UIColor.darkGray


        imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 300)
        imageView.image = UIImage.init(named: "poster")
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        view.addSubview(imageView)
        self.tableView.backgroundColor = .clear
        self.view.bringSubview(toFront: tableView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}


extension ViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomTableViewCell
        switch indexPath.row % 2 {
        case 0:
            cell.titleLabel.text = "Lorem Ipsum is simply dummy text ."
            cell.contentView.backgroundColor = UIColor.darkGray
        default:
            cell.contentView.backgroundColor = UIColor.black
            cell.titleLabel.text = "There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."
            cell.titleLabel.textColor = .white
        }
        return cell
    }

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.lastContentOffset = scrollView.contentOffset.y
    }


    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        if self.lastContentOffset < scrollView.contentOffset.y {
            let y = 220 - (scrollView.contentOffset.y + 220)
            let height = min(max(y, 60), 220)
            if height >= 128 {
                imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height + 70)
            }
        } else if self.lastContentOffset > scrollView.contentOffset.y {
            let y = 300 - (scrollView.contentOffset.y + 300)
            let height = min(max(y, 60), 400)
            imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height + 80)
        }
    }
}