標籤:
iOS UITableViewCell UITableVIewController 純程式碼開發 <原創>1.純程式碼 自訂UITableViewCell 直接上代碼//////#import <UIKit/UIKit.h>@interface CodeTableViewCell : UITableViewCell@property (nonatomic, weak) UIImageView *iconView;@property (nonatomic, weak) UILabel *labName;+ (instancetype)cellWithTableView:(UITableView *)tableView;@end///#import "CodeTableViewCell.h"#define NJNameFont [UIFont systemFontOfSize:15]#define NJTextFont [UIFont systemFontOfSize:16]@implementation CodeTableViewCell+ (instancetype)cellWithTableView:(UITableView *)tableView { // NSLog(@"cellForRowAtIndexPath"); static NSString *identifier = @"status"; // 1.緩衝中取 CodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; // 2.建立 if (cell == nil) { cell = [[CodeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } return cell;} /** * 構造方法(在初始化對象的時候會調用) * 一般在這個方法中添加需要顯示的子控制項 */- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // 讓自訂Cell和系統的cell一樣, 一建立出來就擁有一些子控制項提供給我們使用 // 1.建立頭像 UIImageView *iconView = [[UIImageView alloc] init]; [self.contentView addSubview:iconView]; self.iconView = iconView; // 2.建立暱稱 UILabel *nameLabel = [[UILabel alloc] init]; nameLabel.font = NJNameFont; nameLabel.textColor=[UIColor redColor]; [self.contentView addSubview:nameLabel]; self.labName = nameLabel; [self.contentView setBackgroundColor:[UIColor clearColor]]; [self settingFrame]; } return self;}//設定相對位置- (void)settingFrame{ self.frame = CGRectMake(0, 0, 320, 120); self.labName.frame=CGRectMake(100, 120/2-25, 200, 50); self.iconView.frame=CGRectMake(10, (120-80)/2, 80, 80);}- (void)awakeFromNib { // Initialization code}- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state}@end//////2. 通常寫UITableView 都是 在UIViewController裡面定義一個UITableView,現在直接讓當前控制器繼承於UITableViewController,避免寫繁文縟節的無作用程式碼,更方便.但是table view controller 只限於管理一個全屏展示的 table view。 最後,你需要把遷移後丟失的 table view controller 的特性給補回來。大多數都是 viewWillAppear: 或 viewDidAppear: 中簡單的一條語句。直接上代碼: #import <UIKit/UIKit.h>@interface RootViewController : UITableViewController@end/////#import "RootViewController.h"#import "CodeTableViewCell.h"@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>{ UITableView *tableViewMine;}@end@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor yellowColor];// tableViewMine=[[UITableView alloc]initWithFrame:self.view.frame];// [self.view addSubview:tableViewMine];// tableViewMine.delegate=self;// tableViewMine.dataSource=self;}-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 120;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 6;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat{//調用 自訂的tableViewCell CodeTableViewCell *cell=[CodeTableViewCell cellWithTableView:tableView]; cell.labName.text=@"sssssssssss"; cell.iconView.image=[UIImage imageNamed:@"iconhead.jpg"]; return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"%ld",indexPath.row);}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end///////////
上:
iOS UITableViewCell UITableVIewController 純程式碼開發