114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果)
2015-06-16 11:35
405 查看
关键操作:
[b]效果如下:[/b]
[b]ViewController.h[/b]
ViewController.m
KMTableViewCell.h
KMTableViewCell.m
[b]KMTableViewCell.xib[/b]
[b]AppDelegate.h[/b]
[b]AppDelegate.m[/b]
[b]FriendsInfo.plist[/b]
[b]效果如下:[/b]
[b]ViewController.h[/b]
#import <UIKit/UIKit.h> @interface ViewController : UITableViewController @property (strong, nonatomic) NSMutableArray *mArrDataList; @property (strong, nonatomic) NSMutableArray *mArrImageList; @end
ViewController.m
#import "ViewController.h" #import "KMTableViewCell.h" @interface ViewController () - (void)layoutUI; - (void)loadData; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self layoutUI]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)layoutUI { [self loadData]; self.view.backgroundColor = [UIColor whiteColor]; self.navigationItem.title = @"自定义UITableViewCell"; } - (void)loadData { NSBundle *bundle = [NSBundle mainBundle]; NSURL *urlFriendsInfo = [bundle URLForResource:@"FriendsInfo" withExtension:@"plist"]; NSDictionary *dicFriendsInfo = [NSDictionary dictionaryWithContentsOfURL:urlFriendsInfo]; NSInteger len = [dicFriendsInfo count]; _mArrDataList = [[NSMutableArray alloc] initWithCapacity:len]; _mArrImageList = [[NSMutableArray alloc] initWithCapacity:len]; for (NSInteger i=0; i<len; i++) { NSString *strKey = [NSString stringWithFormat:@"%lu", (unsigned long)(i+1)]; NSDictionary *dicData = [dicFriendsInfo objectForKey:strKey]; [_mArrDataList addObject:dicData]; UIImage *img = [UIImage imageNamed:strKey]; [_mArrImageList addObject:img]; } } #pragma mark - TableView DataSource and Delegate - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"FriendsInfo列表"; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_mArrDataList count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"cellIdentifier"; static BOOL isRegistered = NO; if (!isRegistered) { UINib *nib = [UINib nibWithNibName:@"KMTableViewCell" bundle:nil]; [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier]; isRegistered = YES; } KMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[KMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } NSDictionary *rowData = _mArrDataList[indexPath.row]; cell.name = [rowData objectForKey:@"name"]; cell.desc = [rowData objectForKey:@"desc"]; cell.location = [rowData objectForKey:@"location"]; cell.imgCustom = _mArrImageList[indexPath.row]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60.0; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { // 从锚点位置出发,逆时针绕 Y 和 Z 坐标轴旋转90度 CATransform3D transform3D = CATransform3DMakeRotation(M_PI_2, 0.0, 1.0, 1.0); // 定义 cell 的初始状态 cell.alpha = 0.0; cell.layer.transform = transform3D; cell.layer.anchorPoint = CGPointMake(0.0, 0.5); // 设置锚点位置;默认为中心点(0.5, 0.5) // 定义 cell 的最终状态,执行动画效果 // 方式一:普通操作设置动画 // [UIView beginAnimations:@"transform" context:NULL]; // [UIView setAnimationDuration:0.5]; // cell.alpha = 1.0; // cell.layer.transform = CATransform3DIdentity; // CGRect rect = cell.frame; // rect.origin.x = 0.0; // cell.frame = rect; // [UIView commitAnimations]; // 方式二:代码块设置动画 [UIView animateWithDuration:0.5 animations:^{ cell.alpha = 1.0; cell.layer.transform = CATransform3DIdentity; CGRect rect = cell.frame; rect.origin.x = 0.0; cell.frame = rect; }]; } @end
KMTableViewCell.h
#import <UIKit/UIKit.h> @interface KMTableViewCell : UITableViewCell @property (strong, nonatomic) IBOutlet UIImageView *imgVCustom; @property (strong, nonatomic) IBOutlet UILabel *lblName; @property (strong, nonatomic) IBOutlet UILabel *lblDesc; @property (strong, nonatomic) IBOutlet UILabel *lblLocation; @property (copy, nonatomic) UIImage *imgCustom; @property (copy, nonatomic) NSString *name; @property (copy, nonatomic) NSString *desc; @property (copy, nonatomic) NSString *location; @end
KMTableViewCell.m
#import "KMTableViewCell.h" @implementation KMTableViewCell - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (void)setImgCustom:(UIImage *)imgCustom { if (![_imgCustom isEqual:imgCustom]) { _imgCustom = [imgCustom copy]; _imgVCustom.image = _imgCustom; } } - (void)setName:(NSString *)name { if (![_name isEqualToString:name]) { _name = [name copy]; _lblName.text = _name; } } - (void)setDesc:(NSString *)desc { if (![_desc isEqualToString:desc]) { _desc = [desc copy]; _lblDesc.text = _desc; } } - (void)setLocation:(NSString *)location { if (![_location isEqualToString:location]) { _location = [location copy]; _lblLocation.text = _location; } } @end
[b]KMTableViewCell.xib[/b]
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="7531" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES"> <dependencies> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7520"/> </dependencies> <objects> <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/> <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/> <tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="cellIdentifier" id="KGk-i7-Jjw" customClass="KMTableViewCell"> <rect key="frame" x="0.0" y="0.0" width="320" height="60"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="KGk-i7-Jjw" id="H2p-sc-9uM"> <rect key="frame" x="0.0" y="0.0" width="320" height="43"/> <autoresizingMask key="autoresizingMask"/> <subviews> <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="3Br-R7-YsD"> <rect key="frame" x="10" y="5" width="50" height="50"/> </imageView> <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"> <rect key="frame" x="78" y="19" width="200" height="21"/> <fontDescription key="fontDescription" type="system" pointSize="12"/> <color key="textColor" cocoaTouchSystemColor="darkTextColor"/> <nil key="highlightedColor"/> </label> <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"> <rect key="frame" x="78" y="38" width="42" height="21"/> <fontDescription key="fontDescription" type="system" pointSize="12"/> <color key="textColor" red="0.517578125" green="0.517578125" blue="0.517578125" alpha="1" colorSpace="calibratedRGB"/> <nil key="highlightedColor"/> </label> <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"> <rect key="frame" x="78" y="0.0" width="42" height="21"/> <fontDescription key="fontDescription" type="boldSystem" pointSize="14"/> <color key="textColor" cocoaTouchSystemColor="darkTextColor"/> <nil key="highlightedColor"/> </label> </subviews> </tableViewCellContentView> <connections> <outlet property="imgVCustom" destination="3Br-R7-YsD" id="ODd-v8-Lem"/> <outlet property="lblDesc" destination="dJA-8r-pcJ" id="SFw-6v-VAS"/> <outlet property="lblLocation" destination="5h7-UD-fzl" id="W60-wQ-S2r"/> <outlet property="lblName" destination="kPR-pa-8uG" id="BH7-oj-3Kx"/> </connections> </tableViewCell> </objects> </document>
[b]AppDelegate.h[/b]
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UINavigationController *navigationController; @end
[b]AppDelegate.m[/b]
#import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; ViewController *viewController = [[ViewController alloc] init]; _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; _window.rootViewController = _navigationController; //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无 [_window makeKeyAndVisible]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { } - (void)applicationDidEnterBackground:(UIApplication *)application { } - (void)applicationWillEnterForeground:(UIApplication *)application { } - (void)applicationDidBecomeActive:(UIApplication *)application { } - (void)applicationWillTerminate:(UIApplication *)application { } @end
[b]FriendsInfo.plist[/b]
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>1</key> <dict> <key>name</key> <string>小明</string> <key>desc</key> <string>干啥呢?</string> <key>location</key> <string>广州</string> </dict> <key>2</key> <dict> <key>name</key> <string>痞子</string> <key>desc</key> <string>好好学习,天天向上!</string> <key>location</key> <string>广州</string> </dict> <key>3</key> <dict> <key>name</key> <string>疯子</string> <key>desc</key> <string>倚楼听风雨,淡看江湖路。</string> <key>location</key> <string>广州</string> </dict> <key>4</key> <dict> <key>name</key> <string>梦醒</string> <key>desc</key> <string>书到用时方恨少</string> <key>location</key> <string>广州</string> </dict> <key>5</key> <dict> <key>name</key> <string>落落</string> <key>desc</key> <string>生日快乐!</string> <key>location</key> <string>广州</string> </dict> <key>6</key> <dict> <key>name</key> <string>丫丫</string> <key>desc</key> <string>做个踏实的科研女</string> <key>location</key> <string>广州</string> </dict> <key>7</key> <dict> <key>name</key> <string>乐天平</string> <key>desc</key> <string>在火车上</string> <key>location</key> <string>广州</string> </dict> <key>8</key> <dict> <key>name</key> <string>北暮</string> <key>desc</key> <string>好久不见!</string> <key>location</key> <string>广州</string> </dict> <key>9</key> <dict> <key>name</key> <string>苹果</string> <key>desc</key> <string>喜欢苹果,更喜欢青苹果!</string> <key>location</key> <string>广州</string> </dict> <key>10</key> <dict> <key>name</key> <string>木头</string> <key>desc</key> <string>清心薄欲 静躁作学</string> <key>location</key> <string>广州</string> </dict> <key>11</key> <dict> <key>name</key> <string>醉清风</string> <key>desc</key> <string>一醉解千愁</string> <key>location</key> <string>广州</string> </dict> <key>12</key> <dict> <key>name</key> <string>浅の斯</string> <key>desc</key> <string>想剪短发……剪还是不剪(⊙o⊙)?</string> <key>location</key> <string>广州</string> </dict> <key>13</key> <dict> <key>name</key> <string>虚伪</string> <key>desc</key> <string>讨厌虚伪</string> <key>location</key> <string>广州</string> </dict> <key>14</key> <dict> <key>name</key> <string>阁楼</string> <key>desc</key> <string>窗外的风景。</string> <key>location</key> <string>广州</string> </dict> </dict> </plist>
相关文章推荐
- Axure制作手机UI原型之界面滑动滚动效果
- UE4教程&论坛
- 多线程之-NSOperationQueue
- UI之单例模式
- web.xml中dispatcher标签详解
- (转)通过Chisel加强LLDB的UI调试能力
- 如何使用NSOperations和NSOperationQueues
- 【EasyUI学习-3】Easyui tabs入门实践
- Implement Stack using Queues
- 常用设备类别及其GUID
- IOS UIScrollView中 使用 touch 无法响应的问题
- IOS Key-Value Observing (KVO)
- IOS Key-Value Observing (KVO)
- 【Android UI设计与开发】第09期:底部菜单栏(四)Fragment+PopupWindow仿QQ空间最新版底部菜单栏
- 【uiautomator】UiDevice
- Android stuido debug调试
- VS2008工程移植到2010的问题'system.io.fileloadexception was unhandled message=mixed mode assembly is built ag
- 【Android UI设计与开发】第08期:底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- LeetCode OJ 之 Implement Stack using Queues(使用队列实现栈)
- easy ui 隐藏tabs项