UITableView a custom cell and examples of different heights

Source: Internet
Author: User
Tags reserved uikit

Cell style, two lableview;

Cell

#import <UIKit/UIKit.h>
#import "User.h"
@interface Mycell:uitableviewcell
//Represents the property of the item height. This is computed according to the item content
@property (assign,nonatomic) int heigth;
@property (strong,nonatomic) User *user;
@property (strong,nonatomic) Uilabel *name;
@property (strong,nonatomic) Uilabel *content;
+ (Instancetype) Cellwithtableview: (uitableview*) TableView;
@end

#import "MyCell.h" @implementation MyCell + (instancetype) Cellwithtableview: (uitableview*) tableview{//cell Cache
    MyCell *cell=[tableview dequeuereusablecellwithidentifier:@ "Biaoji"];
    if (cell==nil) {Cell=[[mycell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:@ "Biaoji"];
} return cell;
};
    Method of overriding SetUser-(void) SetUser: (user *) User {_user = user;
     assigning value self.name.text=_user.name;
     Self.content.text=_user.content;
     
     Self.heigth=100+arc4random ()%100-80; }//Rewrite construction Method-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier {SE
    Lf=[super Initwithstyle:style Reuseidentifier:reuseidentifier];
        if (self) {Uilabel *name=[[uilabel alloc]init];
        Name.frame=cgrectmake (20, 0, 50, 30);
        Name.textcolor=[uicolor Bluecolor];
        [Self.contentview Addsubview:name];
        Self.name=name;
      Uilabel *content=[[uilabel Alloc]init];  Content.frame=cgrectmake (60, 0, 50, 30);
        Content.textcolor=[uicolor Bluecolor];
        [Self.contentview addsubview:content];
        
    Self.content=content;
return self;
 } @end
User

#import <Foundation/Foundation.h>

@interface user:nsobject
@property (strong,nonatomic) NSString * Name;
@property (strong,nonatomic) nsstring *content;

@end
#import "User.h"

@implementation User

@end

Viewcontrol:

VIEWCONTROLLER.M//Test_tableviewcell////Created by Shibo on 2017/3/15. copyright©2017 of the Year Shi Bo.
All rights reserved. #import "ViewController.h" #import "User.h" #import "MyCell.h" @interface Viewcontroller () <uitableviewdatasource
,uitableviewdelegate> @end @implementation viewcontroller nsmutablearray *alluser;
UITableView *tableview;
    -(void) viewdidload {alluser=[[nsmutablearray alloc]init];
        for (int i=0;i<10;i++) {User *user=[[user alloc]init];
        User.name=[nsstring stringwithformat:@ "%@%d", @ "", I];
        User.content=[nsstring stringwithformat:@ "%@%d", @ "This is", I];
    [AllUser Addobject:user]; } Tableview=[[uitableview alloc]initwithframe:cgrectmake (0, 0, [UIScreen mainscreen].bounds.size.width, [UIScreen mai
    Nscreen].bounds.size.height)];
    tableview.delegate=self;
   tableview.datasource=self;
    Tableview.backgroundcolor=[uicolor Clearcolor];
    [Self.view Addsubview:tableview];
    [Super Viewdidload]; // Do no additional setup after loading the view, typically from a nib.
    }-(void) didreceivememorywarning {[Super didreceivememorywarning];
Dispose of any of the can is recreated. //Click on the next Quxiao highlight state-(void) Unselectcell: (ID) sender{[tableview Deselectrowatindexpath:[tableview
Indexpathforselectedrow] Animated:yes]; }//Return height-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{MyCell *ce=
    
    [Self Tableview:tableview cellforrowatindexpath:indexpath];
return ce.heigth;
};
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return allUser.count; -(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{[self performselector:@
    Selector (Unselectcell:) Withobject:nil afterdelay:0.5];


NSLog (@ "%@%lu", @ "click to", Indexpath.row); }//Row display. Implementers should *always* try to reuse cells by setting all cell ' s reuseidentifier anD querying for available reusable cells with Dequeuereusablecellwithidentifier://Cell gets various attributes set Automa Tically based on table (separators) and data source (accessory views, editing controls)-(UITableViewCell *) TableView: (U Itableview *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{MyCell-*cell=[mycell CellWithTableview:tablev
    Iew];
    Cell.user=[alluser ObjectAtIndex:indexPath.row];
return cell;

};
 @end
Appdelegate:

//  AppDelegate.h
//  Test_tableviewcell////
Created by Shibo on  2017/3/15.  copyright©2017 of the year Shi Bo. All rights reserved.

#import <UIKit/UIKit.h>

@interface appdelegate:uiresponder <UIApplicationDelegate>
@property (strong,nonatomic) Uinavigationcontroller *na;
@property (Strong, nonatomic) UIWindow *window;


@end

APPDELEGATE.M//Test_tableviewcell////Created by Shibo on 2017/3/15. copyright©2017 of the Year Shi Bo.
All rights reserved. #import "AppDelegate.h" #import "ViewController.h" @interface appdelegate () @end @implementation appdelegate-(B OOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {//
    Override point for customization after application launch.
    Self.window=[[uiwindow Alloc]initwithframe:[[uiscreen Mainscreen] bounds];
    Self.na=[[uinavigationcontroller Alloc]init];
    [Self.window makekeyandvisible];
    Viewcontroller *w=[[viewcontroller Alloc]init];
    Self.window.rootviewcontroller=self.na;
    [Self.na pushviewcontroller:w Animated:yes];
    [Self.window AddSubview:self.na.view];
return YES; }-(void) Applicationwillresignactive: (uiapplication *) application {//Sent when the application was about to move F Rom Active to inactive state. This can occur for certain types of temporary interruptiONS (such as a incoming phone call or SMS message) or the user quits the application and it begins to
    The background state. Use the To pause ongoing tasks, disable timers, and invalidate graphics rendering.
Games should to pause the game. }-(void) Applicationdidenterbackground: (uiapplication *) application {//use ' this ' to release shared resource s, save user data, invalidate timers, and store enough application state information to restore your to its CU
    Rrent state in case it is terminated later. If your application supports background execution, this is called instead of Applicationwillterminate:when the
User quits.  }-(void) Applicationwillenterforeground: (uiapplication *) application {//called as part of the transition from the Background to the active state;
Here you can undo many of the changes made on entering the background. }-(void) Applicationdidbecomeactive: (uiapplication *) Application {//Restart any tasks this were paused (or not yet started) while the application is inactive.
If the application is previously in the background, optionally refresh the user interface. }-(void) Applicationwillterminate: (uiapplication *) application {//called when the application was about to Termina Te. Save data if appropriate.
Also applicationdidenterbackground:.
 } @end





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.