在详细信息标题栏中,显示“ Y的图片X”

时间:2019-04-22 17:58:41

标签: ios swift uitableview

我遇到了3个挑战中的1个问题。

第一个挑战  使用Interface Builder在表格视图单元格中选择文本标签,并将其字体大小调整为更大的字体–试试看,看看效果如何。 (完成)

第二次挑战 在主表格视图中,按排序顺序显示图像名称(完成)

第三个挑战是  “不要在详细标题栏中显示图像名称,而是显示“ Y图片X”,其中Y是图像总数,X是所选图片在数组中的位置。请确保从1而不是0开始计数。

我将创建一个简单的表格视图行,其中包含单元格,图像和单击的图像,当您单击它时,它将为您显示图片。

我有个主意,但是我很挣扎。所以我知道标题将使用字符串插值并且我已经创建了

var selectedPictureNumber = 0
var totalPictures = 0

现在我尝试创建

var selectedPictureNumber =  pictures[indexPath.row]
                     or

var selectedPictureNumber = indexPath.row

但是我收到错误消息

在这里,我在mainViewController.swift上得到了

class ViewController: UITableViewController {

        var pictures = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()


        // Adds a title to the navegation bar.

        title = "Storm Viewer"


        //This makes the title larger as well.
        navigationController?.navigationBar.prefersLargeTitles = true

        // THis code manages the images from the files saved locally to xcode

        let fm = FileManager.default
        let path = Bundle.main.resourcePath!
        let items = try! fm.contentsOfDirectory(atPath: path)


        for item in items {
            if item.hasPrefix("nssl"){
                pictures.append(item)

            }
        }
        // This pictures.sort makes the arrays sort in order.

        pictures.sort()
        print(pictures)
    }

    // This code creates  the table view rows , adds the table cells and adds the pictures to each table view cell.


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return pictures.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "picture", for: indexPath)
        cell.textLabel?.text = pictures[indexPath.row]
        return cell
    }
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController{
            vc.selectedImage = pictures[indexPath.row]
            navigationController?.pushViewController(vc, animated: true)
        }
    }
}


这是我的detailViewController.swift中的内容

class DetailViewController: UIViewController {

    // Outlets

    @IBOutlet var imageView: UIImageView!

    // Vars
    var selectedImage : String?
    var selectedPictureNumber = 0
    var totalPictures = 0

    override func viewDidLoad(){

        title = selectedImage

        super.viewDidLoad()

        // THis code makes the title of the navegation bar on the detailViewcontroller the name of the img.

        title = selectedImage


        // This line will make it so that the title on the navegation bar will not be large.

        navigationItem.largeTitleDisplayMode = .never

        //This code lets the images that are on the detailViewController

        if let imageToLoad = selectedImage{
            imageView.image = UIImage(named: imageToLoad)
        }

    }


    // This code will make the Navigation bar appear & Dissapear with a touch on the screen.

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.hidesBarsOnTap = true
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.hidesBarsOnTap = false
    }
    }

1 个答案:

答案 0 :(得分:2)

我有您的问题,只需按照以下步骤更新mainViewController.swift中的代码:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController{
        vc.selectedImage = pictures[indexPath.row]
        vc.selectedPictureNumber = indexPath.row + 1
        vc.totalPictures = pictures.count
        navigationController?.pushViewController(vc, animated: true)
    }
}

然后在detailViewController.swift文件的内部viewDidLoad方法中添加以下代码。

self.title = "Picture \(selectedPictureNumber) of (totalPictures)"

我希望这是您正在寻找的解决方案。