{"id":7518,"date":"2025-06-02T13:45:29","date_gmt":"2025-06-05T06:42:28","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7518"},"modified":"2025-06-05T13:45:29","modified_gmt":"2025-06-05T06:42:28","slug":"https-badgameshow-com-steven-423","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-423\/","title":{"rendered":"Swift \u81ea\u5b9a\u7fa9 UITableViewCell \u7684\u5b8c\u6574\u6307\u5357\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, UITableViewCell, \u81ea\u5b9a\u7fa9, iOS \u958b\u767c, \u6559\u5b78\"><\/p>\n<p>\u5728 iOS \u958b\u767c\u4e2d\uff0c<strong>UITableViewCell<\/strong> \u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u5143\u4ef6\u3002\u900f\u904e\u81ea\u5b9a\u7fa9\u7684 cell\uff0c\u6211\u5011\u53ef\u4ee5\u81ea\u7531\u6392\u7248\uff0c\u653e\u7f6e\u5404\u7a2e\u5143\u4ef6\uff0c\u5275\u9020\u51fa\u500b\u6027\u5316\u7684\u4ecb\u9762\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Swift \u4e2d\u5275\u5efa\u81ea\u5b9a\u7fa9\u7684 <strong>UITableViewCell<\/strong>\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u6559\u5b78\u6d41\u7a0b\u8207\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h2>\u5982\u4f55\u5728 Swift \u4e2d\u5275\u5efa\u81ea\u5b9a\u7fa9\u7684 UITableViewCell<\/h2>\n<p>\u8981\u5275\u5efa\u4e00\u500b\u81ea\u5b9a\u7fa9\u7684 <strong>UITableViewCell<\/strong>\uff0c\u6211\u5011\u9700\u8981\u9996\u5148\u5275\u5efa\u4e00\u500b <strong>UITableViewCell<\/strong> \u7684\u5b50\u985e\u5225\u3002\u5728\u9019\u500b\u5b50\u985e\u5225\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u81ea\u7531\u653e\u7f6e\u5404\u7a2e\u5143\u4ef6\uff0c\u5982 <strong>UILabel<\/strong>\u3001<strong>UIImageView<\/strong> \u7b49\u7b49\uff0c\u4e26\u9032\u884c\u6392\u7248\u4ee5\u9054\u5230\u60f3\u8981\u7684\u4ecb\u9762\u6548\u679c\u3002<\/p>\n<h3>\u6b65\u9a5f\u4e00\uff1a\u5275\u5efa\u81ea\u5b9a\u7fa9\u7684 UITableViewCell \u5b50\u985e\u5225<\/h3>\n<pre><code class=\"language-swift line-numbers\">class CustomTableViewCell: UITableViewCell {\n    let label = UILabel()\n    let imageView = UIImageView()\n\n    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {\n        super.init(style: style, reuseIdentifier: reuseIdentifier)\n        \n        \/\/ \u8a2d\u5b9a label \u7684\u4f4d\u7f6e\u8207\u5c6c\u6027\n        label.frame = CGRect(x: 15, y: 10, width: 200, height: 20)\n        label.font = UIFont.systemFont(ofSize: 16)\n        contentView.addSubview(label)\n\n        \/\/ \u8a2d\u5b9a imageView \u7684\u4f4d\u7f6e\u8207\u5c6c\u6027\n        imageView.frame = CGRect(x: 15, y: 40, width: 60, height: 60)\n        imageView.contentMode = .scaleAspectFill\n        imageView.clipsToBounds = true\n        contentView.addSubview(imageView)\n    }\n\n    required init?(coder aDecoder: NSCoder) {\n        fatalError(\"init(coder:) has not been implemented\")\n    }\n}\n<\/code><\/pre>\n<h3>\u6b65\u9a5f\u4e8c\uff1a\u5728 UITableView \u4e2d\u4f7f\u7528\u81ea\u5b9a\u7fa9\u7684 Cell<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5728 <strong>UITableView<\/strong> \u7684 delegate \u65b9\u6cd5\u4e2d\u8a2d\u5b9a\u81ea\u5b9a\u7fa9 cell \u7684\u985e\u5225\uff0c\u4e26\u8a2d\u5b9a cell \u7684\u5167\u5bb9\uff0c\u5982 label \u7684\u6587\u5b57\u8207 imageView \u7684\u5716\u7247\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n    let cell = tableView.dequeueReusableCell(withIdentifier: \"CustomTableViewCell\", for: indexPath) as! CustomTableViewCell\n\n    \/\/ \u8a2d\u5b9a cell \u7684\u5167\u5bb9\n    cell.label.text = \"Hello World\"\n    cell.imageView.image = UIImage(named: \"image.png\")\n\n    return cell\n}\n<\/code><\/pre>\n<h3>\u6b65\u9a5f\u4e09\uff1a\u8a2d\u5b9a Cell \u7684\u9ad8\u5ea6<\/h3>\n<p>\u70ba\u4e86\u63a7\u5236 cell \u7684\u9ad8\u5ea6\uff0c\u6211\u5011\u9700\u8981\u5728 <strong>UITableView<\/strong> \u7684 delegate \u65b9\u6cd5\u4e2d\u8a2d\u5b9a cell \u7684\u9ad8\u5ea6\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {\n    return 100 \/\/ \u53ef\u4f9d\u9700\u6c42\u8abf\u6574\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528\u81ea\u5b9a\u7fa9 <strong>UITableViewCell<\/strong> \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u672a\u6b63\u78ba\u8a3b\u518a cell \u985e\u5225\uff1a\u78ba\u4fdd\u5728 <strong>viewDidLoad<\/strong> \u65b9\u6cd5\u4e2d\u4f7f\u7528 <code>tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: \"CustomTableViewCell\")<\/code> \u8a3b\u518a\u3002<\/li>\n<li>\u5716\u7247\u672a\u986f\u793a\uff1a\u78ba\u4fdd\u5716\u7247\u540d\u7a31\u6b63\u78ba\u4e14\u5df2\u52a0\u5165\u5c08\u6848\u8cc7\u6e90\u4e2d\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u81ea\u5b9a\u7fa9 <strong>UITableViewCell<\/strong> \u53ef\u61c9\u7528\u65bc\u8a31\u591a\u5834\u666f\uff0c\u4f8b\u5982\u793e\u4ea4\u5a92\u9ad4\u61c9\u7528\u4e2d\u7684\u5e16\u5b50\u986f\u793a\u3001\u5546\u54c1\u5217\u8868\u7b49\u3002\u4f60\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u6dfb\u52a0\u66f4\u591a\u5143\u4ef6\uff0c\u4f8b\u5982\u6309\u9215\u3001\u661f\u7d1a\u8a55\u50f9\u7b49\uff0c\u4ee5\u63d0\u5347\u7528\u6236\u4e92\u52d5\u6027\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u81ea\u5b9a\u7fa9-UITableViewCell_1675476960.634092.webp\" alt=\"Swift \u81ea\u5b9a\u7fa9 UITableViewCell\" title=\"Swift\u81ea\u5b9a\u7fa9UITableViewCell\u3001Swift\u8a9e\u8a00\u3001UITableViewCell\u3001\u81ea\u5b9a\u7fa9\u61c9\u7528\u7a0b\u5e8f\u3001\u63a7\u5236\u5916\u89c0\u884c\u70ba\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5728 UITableView \u4e2d\u6dfb\u52a0\u591a\u500b\u81ea\u5b9a\u7fa9\u7684 UITableViewCell\uff1f<\/h3>\n<p>\u4f60\u53ef\u4ee5\u5275\u5efa\u591a\u500b\u4e0d\u540c\u7684 cell \u985e\u5225\uff0c\u4e26\u5728 <code>cellForRowAt<\/code> \u65b9\u6cd5\u4e2d\u6839\u64da <code>indexPath.row<\/code> \u8fd4\u56de\u4e0d\u540c\u7684 cell\u3002<\/p>\n<h3>2. UITableViewCell \u7684\u91cd\u7528\u6a5f\u5236\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f<\/h3>\n<p>UITableView \u4f7f\u7528\u91cd\u7528\u6a5f\u5236\u4f86\u63d0\u9ad8\u6027\u80fd\uff0c\u7576 cell \u6efe\u51fa\u5c4f\u5e55\u6642\uff0c\u6703\u88ab\u91cd\u7528\u4ee5\u986f\u793a\u65b0\u7684\u5167\u5bb9\uff0c\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u5167\u5b58\u4f7f\u7528\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u81ea\u5b9a\u7fa9UITableViewCell\uff0c\u8b93\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u81ea\u5b9a\u7fa9\u7684UITableViewCell\u6dfb\u52a0\u5230\u4f60\u7684\u61c9\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4e26\u66f4\u597d\u5730\u63a7\u5236\u4f60\u7684\u61c9\u7528\u7a0b\u5e8f\u7684\u5916\u89c0\u548c\u884c\u70ba\u3002<\/p>\n","protected":false},"author":1,"featured_media":7517,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-7518","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u81ea\u5b9a\u7fa9-UITableViewCell_1675476960.634092.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Xg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=7518"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7518\/revisions"}],"predecessor-version":[{"id":13631,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7518\/revisions\/13631"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7517"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}