Swift - 给表格的单元格UITableViewCell添加图片

表格UITableView中,每一单元格都是一个UITableViewCell。其支持简单的自定义,比如在单元格的内部,添加图片和详细文本标签。
 
注意UITableViewCell的style:
(1) UITableViewCellStyle.Default:默认的,只有一个常规内容标签和一个可选的UIImageView
(2) UITableViewCellStyle.Value1:内容标签在左,详细标签在右,右边是蓝色或灰色的文本
(3) UITableViewCellStyle.Value2:同Value1位置相同,左边是蓝色文本
(4) UITableViewCellStyle.Subtitle:标签上下放置

效果图如下:
  
原文:Swift - 给表格的单元格UITableViewCell添加图片,详细文本标签


代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import  UIKit
 
class  ViewController UIViewController  UITableViewDelegate UITableViewDataSource {
     
     var  tableView: UITableView ?
     
     var  allnames: Dictionary < Int , [ String ]>?
     
     var  adHeaders:[ String ]?
     
     override  func  loadView() {
         super .loadView()
     }
     
     override  func  viewDidLoad() {
         super .viewDidLoad()
         
         //初始化数据,这一次数据,我们放在属性列表文件里
         self .allnames =  [
             0:[ String ]([
                 "UILabel 标签" ,
                 "UITextField 文本框" ,
                 "UIButton 按钮" ]),
             1:[ String ]([
                 "UIDatePiker 日期选择器" ,
                 "UIToolbar 工具条" ,
                 "UITableView 表格视图" ])
         ];
         
         print ( self .allnames)
         
         self .adHeaders = [
             "常见 UIKit 控件" ,
             "高级 UIKit 控件"
         ]
         
         //创建表视图
         self .tableView =  UITableView (frame: self .view.frame, style: UITableViewStyle . Grouped )
         self .tableView!.delegate =  self
         self .tableView!.dataSource =  self
         //创建一个重用的单元格
         self .tableView!.registerClass( UITableViewCell . self , forCellReuseIdentifier:  "SwiftCell" )
         self .view.addSubview( self .tableView!)
         
         //创建表头标签
         let  headerLabel =  UILabel (frame:  CGRectMake (0, 0,  self .view.bounds.size.width, 30))
         headerLabel.backgroundColor =  UIColor .blackColor()
         headerLabel.textColor =  UIColor .whiteColor()
         headerLabel.numberOfLines = 0
         headerLabel.lineBreakMode =  NSLineBreakMode . ByWordWrapping
         headerLabel.text =  "高级 UIKit 控件"
         headerLabel.font =  UIFont .italicSystemFontOfSize(20)
         self .tableView!.tableHeaderView = headerLabel
     }
     
     //在本例中,有2个分区
     func  numberOfSectionsInTableView(tableView:  UITableView ) ->  Int  {
         return  2;
     }
     
     //返回表格行数(也就是返回控件数)
     func  tableView(tableView:  UITableView , numberOfRowsInSection section:  Int ) ->  Int  {
         let  data =  self .allnames?[section]
         return  data!.count
     }
     
     
     // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部
     func  tableView(tableView: UITableView , titleForHeaderInSection
         section: Int )-> String ?
     {
         var  headers =   self .adHeaders!;
         return  headers[section];
     }
     // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的尾部
     func  tableView(tableView: UITableView , titleForFooterInSection
         section: Int )-> String ?
     {
         let  data =  self .allnames?[section]
         return  "有\(data!.count)个控件"
     }
     
     
     //创建各单元显示内容(创建参数indexPath指定的单元)
     func  tableView(tableView:  UITableView , cellForRowAtIndexPath indexPath:  NSIndexPath )
         ->  UITableViewCell
     {
         //为了提供表格显示性能,已创建完成的单元需重复使用
         let  identify: String  "SwiftCell"
         
         //同一形式的单元格重复使用,在声明时已注册
         let  secno = indexPath.section
         let  data =  self .allnames?[secno]
         if (secno == 0)
         {
             let  cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath)
                 as  UITableViewCell
             cell.accessoryType =  UITableViewCellAccessoryType . DisclosureIndicator
             
             let  image =  UIImage (named: "heart.png" )
             cell.imageView?.image = image
             cell.textLabel?.text = data![indexPath.row]
             
             return  cell
         }
         else
         {
             //第二个分组表格使用详细标签
             let  adcell =  UITableViewCell (style:  UITableViewCellStyle . Subtitle ,
                 reuseIdentifier:  "SwiftCell" )
             adcell.textLabel?.text = data![indexPath.row]
             print (adcell.textLabel?.text)
             adcell.detailTextLabel!.text =  "这是\(data![indexPath.row])的说明"
             
             return  adcell;
         }
     }
     
     // UITableViewDelegate 方法,处理列表项的选中事件
     func  tableView(tableView:  UITableView , didSelectRowAtIndexPath indexPath:  NSIndexPath )
     {
         self .tableView!.deselectRowAtIndexPath(indexPath, animated:  true )
         
         let  itemString =  self .allnames![indexPath.section]![indexPath.row]
         
         let  alertview =  UIAlertView ();
         alertview.title =  "提示!"
         alertview.message =  "你选中了【\(itemString)】" ;
         alertview.addButtonWithTitle( "确定" )
         alertview.show();       
     }
     
     override  func  didReceiveMemoryWarning() {
         super .didReceiveMemoryWarning()
     }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值