首页 > 学院 > 开发设计 > 正文

114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果)

2019-11-14 18:50:47
字体:
来源:转载
供稿:网友

关键操作:

效果如下:

ViewController.h

1 #import <UIKit/UIKit.h>2 3 @interface ViewController : UITableViewController4 @PRoperty (strong, nonatomic) NSMutableArray *mArrDataList;5 @property (strong, nonatomic) NSMutableArray *mArrImageList;6 7 @end

ViewController.m

  1 #import "ViewController.h"  2 #import "KMTableViewCell.h"  3   4 @interface ViewController ()  5 - (void)layoutUI;  6 - (void)loadData;  7 @end  8   9 @implementation ViewController 10  11 - (void)viewDidLoad { 12     [super viewDidLoad]; 13      14     [self layoutUI]; 15 } 16  17 - (void)didReceiveMemoryWarning { 18     [super didReceiveMemoryWarning]; 19     // Dispose of any resources that can be recreated. 20 } 21  22  23 - (void)layoutUI { 24     [self loadData]; 25      26     self.view.backgroundColor = [UIColor whiteColor]; 27     self.navigationItem.title = @"自定义UITableViewCell"; 28 } 29  30 - (void)loadData { 31     NSBundle *bundle = [NSBundle mainBundle]; 32     NSURL *urlFriendsInfo = [bundle URLForResource:@"FriendsInfo" withExtension:@"plist"]; 33     NSDictionary *dicFriendsInfo = [NSDictionary dictionaryWithContentsOfURL:urlFriendsInfo]; 34     NSInteger len = [dicFriendsInfo count]; 35     _mArrDataList = [[NSMutableArray alloc] initWithCapacity:len]; 36     _mArrImageList = [[NSMutableArray alloc] initWithCapacity:len]; 37     for (NSInteger i=0; i<len; i++) { 38         NSString *strKey = [NSString stringWithFormat:@"%lu", (unsigned long)(i+1)]; 39         NSDictionary *dicData = [dicFriendsInfo objectForKey:strKey]; 40         [_mArrDataList addObject:dicData]; 41          42         UIImage *img = [UIImage imageNamed:strKey]; 43         [_mArrImageList addObject:img]; 44     } 45 } 46  47 #pragma mark - TableView DataSource and Delegate 48 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 49     return @"FriendsInfo列表"; 50 } 51  52 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 53     return 1; 54 } 55  56 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 57     return [_mArrDataList count]; 58 } 59  60 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 61     static NSString *cellIdentifier = @"cellIdentifier"; 62     static BOOL isRegistered = NO; 63     if (!isRegistered) { 64         UINib *nib = [UINib nibWithNibName:@"KMTableViewCell" bundle:nil]; 65         [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier]; 66         isRegistered = YES; 67     } 68     KMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 69     if (!cell) { 70         cell = [[KMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 71     } 72      73     NSDictionary *rowData = _mArrDataList[indexPath.row]; 74     cell.name = [rowData objectForKey:@"name"]; 75     cell.desc = [rowData objectForKey:@"desc"]; 76     cell.location = [rowData objectForKey:@"location"]; 77     cell.imgCustom = _mArrImageList[indexPath.row]; 78     return cell; 79 } 80  81 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 82     return 60.0; 83 } 84  85 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 86     // 从锚点位置出发,逆时针绕 Y 和 Z 坐标轴旋转90度 87     CATransform3D transform3D = CATransform3DMakeRotation(M_PI_2, 0.0, 1.0, 1.0); 88      89     // 定义 cell 的初始状态 90     cell.alpha = 0.0; 91     cell.layer.transform = transform3D; 92     cell.layer.anchorPoint = CGPointMake(0.0, 0.5); // 设置锚点位置;默认为中心点(0.5, 0.5) 93      94     // 定义 cell 的最终状态,执行动画效果 95     // 方式一:普通操作设置动画 96 //    [UIView beginAnimations:@"transform" context:NULL]; 97 //    [UIView setAnimationDuration:0.5]; 98 //    cell.alpha = 1.0; 99 //    cell.layer.transform = CATransform3DIdentity;100 //    CGRect rect = cell.frame;101 //    rect.origin.x = 0.0;102 //    cell.frame = rect;103 //    [UIView commitAnimations];104     105     // 方式二:代码块设置动画106     [UIView animateWithDuration:0.5 animations:^{107         cell.alpha = 1.0;108         cell.layer.transform = CATransform3DIdentity;109         CGRect rect = cell.frame;110         rect.origin.x = 0.0;111         cell.frame = rect;112     }];113 }114 115 @end

KMTableViewCell.h

 1 #import <UIKit/UIKit.h> 2  3 @interface KMTableViewCell : UITableViewCell 4 @property (strong, nonatomic) IBOutlet UIImageView *imgVCustom; 5 @property (strong, nonatomic) IBOutlet UILabel *lblName; 6 @property (strong, nonatomic) IBOutlet UILabel *lblDesc; 7 @property (strong, nonatomic) IBOutlet UILabel *lblLocation; 8 @property (copy, nonatomic) UIImage *imgCustom; 9 @property (copy, nonatomic) NSString *name;10 @property (copy, nonatomic) NSString *desc;11 @property (copy, nonatomic) NSString *location;12 13 @end

KMTableViewCell.m

 1 #import "KMTableViewCell.h" 2  3 @implementation KMTableViewCell 4  5 - (void)awakeFromNib { 6     // Initialization code 7 } 8  9 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {10     [super setSelected:selected animated:animated];11     // Configure the view for the selected state12 }13 14 - (void)setImgCustom:(UIImage *)imgCustom {15     if (![_imgCustom isEqual:imgCustom]) {16         _imgCustom = [imgCustom copy];17         _imgVCustom.image = _imgCustom;18     }19 }20 21 - (void)setName:(NSString *)name {22     if (![_name isEqualToString:name]) {23         _name = [name copy];24         _lblName.text = _name;25     }26 }27 28 - (void)setDesc:(NSString *)desc {29     if (![_desc isEqualToString:desc]) {30         _desc = [desc copy];31         _lblDesc.text = _desc;32     }33 }34 35 - (void)setLocation:(NSString *)location {36     if (![_location isEqualToString:location]) {37         _location = [location copy];38         _lblLocation.text = _location;39     }40 }41 42 @end

KMTableViewCell.xib

 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 2 <document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="7531" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyaccessControl="none" useAutolayout="YES" useTraitCollections="YES"> 3     <dependencies> 4         <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7520"/> 5     </dependencies> 6     <objects> 7         <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/> 8         <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/> 9         <tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="cellIdentifier" id="KGk-i7-Jjw" customClass="KMTableViewCell">10             <rect key="frame" x="0.0" y="0.0" width="320" height="60"/>11             <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>12             <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="KGk-i7-Jjw" id="H2p-sc-9uM">13                 <rect key="frame" x="0.0" y="0.0" width="320" height="43"/>14                 <autoresizingMask key="autoresizingMask"/>15                 <subviews>16                     <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="3Br-R7-YsD">17                         <rect key="frame" x="10" y="5" width="50" height="50"/>18                     </imageView>19                     <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dJA-8r-pcJ">20                         <rect key="frame" x="78" y="19" width="200" height="21"/>21                         <fontDescription key="fontDescription" type="system" pointSize="12"/>22                         <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>23                         <nil key="highlightedColor"/>24                     </label>25                     <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="5h7-UD-fzl">26                         <rect key="frame" x="78" y="38" width="42" height="21"/>27                         <fontDescription key="fontDescription" type="system" pointSize="12"/>28                         <color key="textColor" red="0.517578125" green="0.517578125" blue="0.517578125" alpha="1" colorSpace="calibratedRGB"/>29                         <nil key="highlightedColor"/>30                     </label>31                     <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="kPR-pa-8uG">32                         <rect key="frame" x="78" y="0.0" width="42" height="21"/>33                         <fontDescription key="fontDescription" type="boldSystem" pointSize="14"/>34                         <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>35                         <nil key="highlightedColor"/>36                     </label>37                 </subviews>38             </tableViewCellContentView>39             <connections>40                 <outlet property="imgVCustom" destination="3Br-R7-YsD" id="ODd-v8-Lem"/>41                 <outlet property="lblDesc" destination="dJA-8r-pcJ" id="SFw-6v-VAS"/>42                 <outlet property="lblLocation" destination="5h7-UD-fzl" id="W60-wQ-S2r"/>43                 <outlet property="lblName" destination="kPR-pa-8uG" id="BH7-oj-3Kx"/>44             </connections>45         </tableViewCell>46     </objects>47 </document>

AppDelegate.h

1 #import <UIKit/UIKit.h>2 3 @interface AppDelegate : UIResponder <UIapplicationDelegate>4 @property (strong, nonatomic) UIWindow *window;5 @property (strong, nonatomic) UINavigationController *navigationController;6 7 @end

AppDelegate.m

 1 #import "AppDelegate.h" 2 #import "ViewController.h" 3  4 @interface AppDelegate () 5 @end 6  7 @implementation AppDelegate 8  9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];11     ViewController *viewController = [[ViewController alloc] init];12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];13     _window.rootViewController = _navigationController;14     //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无15     [_window makeKeyAndVisible];16     return YES;17 }18 19 - (void)applicationWillResignActive:(UIApplication *)application {20 }21 22 - (void)applicationDidEnterBackground:(UIApplication *)application {23 }24 25 - (void)applicationWillEnterForeground:(UIApplication *)application {26 }27 28 - (void)applicationDidBecomeActive:(UIApplication *)application {29 }30 31 - (void)applicationWillTerminate:(UIApplication *)application {32 }33 34 @end

FriendsInfo.plist

  1 <?xml version="1.0" encoding="UTF-8"?>  2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  3 <plist version="1.0">  4 <dict>  5     <key>1</key>  6     <dict>  7         <key>name</key>  8         <string>小明</string>  9         <key>desc</key> 10         <string>干啥呢?</string> 11         <key>location</key> 12         <string>广州</string> 13     </dict> 14     <key>2</key> 15     <dict> 16         <key>name</key> 17         <string>痞子</string> 18         <key>desc</key> 19         <string>好好学习,天天向上!</string> 20         <key>location</key> 21         <string>广州</string> 22     </dict> 23     <key>3</key> 24     <dict> 25         <key>name</key> 26         <string>疯子</string> 27         <key>desc</key> 28         <string>倚楼听风雨,淡看江湖路。</string> 29         <key>location</key> 30         <string>广州</string> 31     </dict> 32     <key>4</key> 33     <dict> 34         <key>name</key> 35         <string>梦醒</string> 36         <key>desc</key> 37         <string>书到用时方恨少</string> 38         <key>location</key> 39         <string>广州</string> 40     </dict> 41     <key>5</key> 42     <dict> 43         <key>name</key> 44         <string>落落</string> 45         <key>desc</key> 46         <string>生日快乐!</string> 47         <key>location</key> 48         <string>广州</string> 49     </dict> 50     <key>6</key> 51     <dict> 52         <key>name</key> 53         <string>丫丫</string> 54         <key>desc</key> 55         <string>做个踏实的科研女</string> 56         <key>location</key> 57         <string>广州</string> 58     </dict> 59     <key>7</key> 60     <dict> 61         <key>name</key> 62         <string>乐天平</string> 63         <key>desc</key> 64         <string>在火车上</string> 65         <key>location</key> 66         <string>广州</string> 67     </dict> 68     <key>8</key> 69     <dict> 70         <key>name</key> 71         <string>北暮</string> 72         <key>desc</key> 73         <string>好久不见!</string> 74         <key>location</key> 75         <string>广州</string> 76     </dict> 77     <key>9</key> 78     <dict> 79         <key>name</key> 80         <string>苹果</string> 81         <key>desc</key> 82         <string>喜欢苹果,更喜欢青苹果!</string> 83         <key>location</key> 84         <string>广州</string> 85     </dict> 86     <key>10</key> 87     <dict> 88         <key>name</key> 89         <string>木头</string> 90         <key>desc</key> 91         <string>清心薄欲 静躁作学</string> 92         <key>location</key> 93         <string>广州</string> 94     </dict> 95     <key>11</key> 96     <dict> 97         <key>name</key> 98         <string>醉清风</string> 99         <key>desc</key>100         <string>一醉解千愁</string>101         <key>location</key>102         <string>广州</string>103     </dict>104     <key>12</key>105     <dict>106         <key>name</key>107         <string>浅の斯</string>108         <key>desc</key>109         <string>想剪短发……剪还是不剪(⊙o⊙)?</string>110         <key>location</key>111         <string>广州</string>112     </dict>113     <key>13</key>114     <dict>115         <key>name</key>116         <string>虚伪</string>117         <key>desc</key>118         <string>讨厌虚伪</string>119         <key>location</key>120         <string>广州</string>121     </dict>122     <key>14</key>123     <dict>124         <key>name</key>125         <string>阁楼</string>126         <key>desc</key>127         <string>窗外的风景。</string>128         <key>location</key>129         <string>广州</string>130     </dict>131 </dict>132 </plist>

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表