Xcode - storyboard_OC版 04:TableViewCell(数据绑定:属性)

TableViewCell

1 - 上篇我们了解 tag 传值,但是在开发中并不是很喜欢使用 tag 来访问视图,我们完全可以把这些视图链接到输出口,利用属性来使用它们。下面我们新建 UITableViewCell 子类 WarriorCell。回到 storyboard 中选中 cell,将其 Class 修改成 WarriorCell:这就意味着当你向 TableView 请求 dequeueReusableCellWithIdentifier 时它就会返回一个 WarriorCell 实例。注:Identifier 的使用 WarriorCell 同类名保持一致,这样看起来更加规范

2 - 同控件建立链接:现在你可以将两个 Label 和 ImageView 分别拖进到 WarriorCell 中。链接完毕之后应该是这个样子

3 - 现在只需要在  WarriorsViewController.m 文件中绑定数据

复制代码
 1 #import "WarriorsViewController.h"
 2 #import "Warrior.h"
 3 #import "WarriorCell.h"
 4 @implementation WarriorsViewController{
 5     NSMutableArray *_warriorsArray;
 6 }
 7 
 8 - (void)viewDidLoad {
 9     [super viewDidLoad];
10     // 创建数组
11     _warriorsArray = [NSMutableArray arrayWithCapacity:0];
12     
13     // 制造数据
14     Warrior *player1 = [[Warrior alloc] init];
15     player1.nameStr = @"カカロット";
16     player1.fightingCapacityStr = [NSString stringWithFormat:@"戦力:%d",FC_Base];
17     player1.rating = 1;
18     [_warriorsArray addObject:player1];
19     
20     player1 = [[Warrior alloc] init];
21     player1.nameStr = @"セクシー";
22     player1.fightingCapacityStr= [NSString stringWithFormat:@"戦力:%d",FC_Base];
23     player1.rating = 2;
24     [_warriorsArray addObject:player1];
25     
26     player1 = [[Warrior alloc] init];
27     player1.nameStr = @"ベジータ";
28     player1.fightingCapacityStr = [NSString stringWithFormat:@"戦力:%d",FC_Base];
29     player1.rating = 3;
30     [_warriorsArray addObject:player1];
31 }
32 
33 #pragma mark - Table view data source
34 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
35 
36     return 1;
37 }
38 
39 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
40 
41     return _warriorsArray.count;
42 }
43 
44 // 直接使用属性
45 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
46 
47     WarriorCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WarriorCell" forIndexPath:indexPath];
48     Warrior *player = [_warriorsArray objectAtIndex:indexPath.row];
49     cell.warriorName.text = player.nameStr;
50     cell.warriorValues.text = player.fightingCapacityStr;
51     cell.warriorIcon.layer.masksToBounds = YES;
52     cell.warriorIcon.layer.cornerRadius = 3.2;
53     cell.warriorIcon.image = [self imageForRating:player.rating];
54     return cell;
55 }
56 
57 // 选取图片
58 - (UIImage *)imageForRating:(int)rating{
59 
60     switch (rating){
61         case 1: return [UIImage imageNamed:@"11.png"];
62         case 2: return [UIImage imageNamed:@"22.png"];
63         case 3: return [UIImage imageNamed:@"33.png"];
64     }
65     return nil;
66 }
67 @end
复制代码

运行效果

4 -  编辑:删除

1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
2     if (editingStyle == UITableViewCellEditingStyleDelete){
3         // 删除数据
4         [_warriorsArray removeObjectAtIndex:indexPath.row];
5         // 删除UI
6         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
7     }
8 }

运行效果:编辑中...  |  删除完成

  

 

posted on   低头捡石頭  阅读(85)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示