UItableview一个自定义cell和不同高度的例子

cell样式,两个lableview;

cell:

#import <UIKit/UIKit.h>
#import "User.h"
@interface MyCell : UITableViewCell
//表示item高度的属性,这个根据item内容计算得到
@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 的缓存
    
    MyCell *cell=[tableview dequeueReusableCellWithIdentifier:@"biaoji"];
    if(cell==nil){
        cell=[[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"biaoji"];
    }
    return cell;
};
//重写setuser的方法
- (void)setUser:(User *)user
 {
         _user = user;
    //赋值
     self.name.text=_user.name;
     self.content.text=_user.content;
     self.heigth=100+arc4random()%100-80;
     
     }
//重写构造方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[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 石博 on 2017/3/15.
//  Copyright © 2017年 石博. 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 mainScreen].bounds.size.height) ];
    tableview.delegate=self;
    tableview.dataSource=self;
   // tableview.backgroundColor=[UIColor clearColor];
    [self.view addSubview:tableview];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//点击后一处quxiao高亮状态
-(void)unselectCell:(id)sender{
    [tableview deselectRowAtIndexPath:[tableview indexPathForSelectedRow] animated:YES];
}
//返回高度
- (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",@"点击到",indexPath.row);


}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    MyCell *cell=[MyCell cellWithTableview:tableview];
    cell.user=[allUser objectAtIndex:indexPath.row];
    return cell;
};

@end
appdelegate:

//
//  AppDelegate.h
//  Test_tableviewCell
//
//  Created by 石博 on 2017/3/15.
//  Copyright © 2017年 石博. 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 石博 on 2017/3/15.
//  Copyright © 2017年 石博. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)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 is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method 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 that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值