iOS UITableViewCell UITableVIewController 純程式碼開發

來源:互聯網
上載者:User

標籤:

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 純程式碼開發

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.