標籤:自訂儲存格 uitableviewcell ios
寫在前面
今天看新聞,科比肩部撕裂,可能會提前退役,這個頑固的男人,終於要落幕了,雖然我不是他的球迷,也是心生敬仰,今天的主題就以科比為素材,向這位人生的鬥士致敬。
前面我們講到了tableview的單元格的系統內建的image label,用起來很方便,可是畢竟限制很多,這一篇將會講到一個神奇的東西——自訂單元格,由你控制單元格顯示的內容,位置,形式。如,我們要製作一個球星列表,需要四項資訊:頭像+姓名+年齡+性別
設定介面
拖控制項,如設定單元格高度,這裡要講一下高度有兩個:
- 設定tableview 的row 高度為:180 這個高度是tableview的行高,就是不管你要多大的cell,螢幕上都只能佔一行的高度……
- 設定cell高度,——這個是cell本身的高度
在介面設定複用:
添加自訂控制項類
添加自訂控制項類:繼承tableviewcell
<span style="font-size:14px;">#import <UIKit/UIKit.h>@interface CustomTableViewCell : UITableViewCell@property (weak, nonatomic) IBOutlet UIImageView *image;@property (weak, nonatomic) IBOutlet UILabel *name;@property (weak, nonatomic) IBOutlet UILabel *age;@property (weak, nonatomic) IBOutlet UILabel *phone;@end</span>
選擇cell,讓關聯自訂類和storyboard
在tableview裡面對自訂的類進行操作即可
#import "CustomTableViewController.h"#import "CustomTableViewCell.h"@interface CustomTableViewController ()<UITableViewDataSource>@end@implementation CustomTableViewController- (void)viewDidLoad { [super viewDidLoad];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return 1;}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *[email protected]"Cell"; CustomTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; [email protected]"科比"; [email protected]"37"; [email protected]"男"; cell.image.image=[UIImage imageNamed:@"Kobe.png"]; return cell;}
相關代碼
http://git.oschina.net/zhengaoxing/table1-type
歡迎轉載,轉載請註明出處
本文地址:http://blog.csdn.net/zhenggaoxing/article/details/43086621
43086621
IOS學習之——表視圖3 自訂儲存格