在 iOS 開發中,UITableViewCell 是一個非常重要的元件,它可以讓我們自定義每一個 cell 的外觀,讓我們可以自由的排版,放置各種元件,讓我們可以自由的創造出我們想要的介面。在 Swift 中,我們可以使用自定義的 UITableViewCell 來創造出我們想要的介面,並且可以讓我們的 App 更加的有個性化。

在 Swift 中,我們可以使用自定義的 UITableViewCell 來創造出我們想要的介面,並且可以讓我們的 App 更加的有個性化。要創建一個自定義的 UITableViewCell,我們需要先創建一個 UITableViewCell 的子類別,並且在這個子類別中,我們可以自由的放置各種元件,例如 UILabel、UIImageView 等等,並且可以自由的排版,讓我們可以創造出我們想要的介面。

如何在 Swift 中創建自定義的 UITableViewCell

要在 Swift 中創建自定義的 UITableViewCell,我們需要先創建一個 UITableViewCell 的子類別,並且在這個子類別中,我們可以自由的放置各種元件,例如 UILabel、UIImageView 等等,並且可以自由的排版,讓我們可以創造出我們想要的介面。

為了創建一個自定義的 UITableViewCell,我們需要先創建一個 UITableViewCell 的子類別,並且在這個子類別中,我們可以自由的放置各種元件,例如 UILabel、UIImageView 等等,並且可以自由的排版,讓我們可以創造出我們想要的介面。

class CustomTableViewCell: UITableViewCell {
    let label = UILabel()
    let imageView = UIImageView()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        // 設定 label 的位置
        label.frame = CGRect(x: 0, y: 0, width: 100, height: 30)
        contentView.addSubview(label)

        // 設定 imageView 的位置
        imageView.frame = CGRect(x: 0, y: 30, width: 100, height: 30)
        contentView.addSubview(imageView)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

接著,我們需要在 UITableView 的 delegate 中,設定 cell 的類別,並且設定 cell 的內容,例如 label 的文字、imageView 的圖片等等,讓我們可以創造出我們想要的介面。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell

    // 設定 cell 的內容
    cell.label.text = "Hello World"
    cell.imageView.image = UIImage(named: "image.png")

    return cell
}

最後,我們需要在 UITableView 的 delegate 中,設定 cell 的高度,讓我們可以自由的控制 cell 的高度,讓我們可以創造出我們想要的介面。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 60
}

在 Swift 中,我們可以使用自定義的 UITableViewCell 來創造出我們想要的介面,並且可以讓我們的 App 更加的有個性化。使用自定義的 UITableViewCell,我們可以自由的放置各種元件,並且可以自由的排版,讓我們可以創造出我們想要的介面。

推薦閱讀文章

推薦閱讀文章

            <a href="https://www.raywenderlich.com/818-customizing-a-uitableview-s-cells">1. Customizing a UITableView’s Cells</a><br>
            <a href="https://www.appcoda.com/customize-table-view-cells-for-uitableview/">2. How to Customize Table View Cells for UITableView</a><br>
            <a href="https://www.hackingwithswift.com/example-code/uikit/how-to-create-a-custom-uitableviewcell-with-swift">3. How to create a custom UITableViewCell with Swift</a><br>
            <a href="https://www.ioscreator.com/tutorials/custom-uitableviewcell-tutorial-ios8-swift">4. Custom UITableViewCell Tutorial in Swift and iOS8</a><br>
            <a href="https://www.youtube.com/watch?v=3Rrzm9ZXdds">5. Swift: Custom UITableViewCell</a

延伸閱讀本站文章

更多swift相關文章

推薦學習youtube影片

[Swift]52.UITableViewCell 自定义表单元格.mp4

Swift 自定義 UITableViewCell

Categorized in:

Tagged in:

,