iOS学习之UiTableView仿个人中心页面,据说学会这个控件就能装逼了

苹果电脑真心不习惯啊,一边开发使windows一边是Mac 都不知道按键盘哪个键了。这UiTableView根android ListView一样重要,等学会这个以后我就出去装逼,但愿装逼不要被大神鄙视,哎,没办法,半路出家,谁让我大学学的机械专业,还这么笨的!如果人生能出来,我要当富二代!好了,不多说了,看贱:不对,看图:

技术分享


卧槽,咋这么大,不管了,男人不都喜欢大吗?

看代码: .h文件里面没东西的我就不贴了:

这是主要代码:

得实现两个协议,根android里面的接口差不多:

UITableViewDataSource

UITableViewDelegate

//
//  MyUiTabview.m
//  MyUItableView
//
//  Created by xiaoyuan on 15/4/24.
//  Copyright (c) 2015年 xiaoyuan. All rights reserved.
//

#import "MyUiTabview.h"
#import "MyCellTableViewCell.h"

@interface MyUiTabview ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView*my;
    UIImageView*head;
    UILabel*name;
    int TB_SECTION_COLLECT;
    int TB_SECTION_UPDATA;
    int TB_SECTION_SETTING;
    int TB_SECTION_QUIT;
    
    int totalSectionCount;
}
@property(retain,nonatomic) NSArray*titles;
@property(retain,nonatomic) NSArray*pic;

@end

@implementation MyUiTabview
@synthesize titles;
@synthesize pic;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //加navigationController 不透明
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor =[UIColor redColor];
    [self.navigationController.navigationBar setTranslucent:NO];
    UILabel *title =[[UILabel alloc] initWithFrame:CGRectZero];
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    title.backgroundColor =[UIColor clearColor];
    self.navigationItem.titleView = title;
    title.text=@"个人中心";
    [title sizeToFit];
    [self iniview];
    my = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    my.backgroundColor =[UIColor whiteColor];
    my.separatorStyle = UITableViewCellEditingStyleNone;
    [self.view addSubview:my];
    my.delegate = self;
    my.dataSource =self;
    [self populateTableHeader];
    
}
//初始化数据
-(void)iniview{
    
    totalSectionCount = 4;
    
    TB_SECTION_COLLECT = 0;
    TB_SECTION_UPDATA = 1;
    TB_SECTION_SETTING = 2;
    TB_SECTION_QUIT = 3;
    [self setTitles:@[@[@"摇一摇",@"朋友圈",@"设置"],@[@"个人中心"],@[@"微信"]]];
    [self setPic:@[@[@"",@"",@""],@[@""],@[@""]]];


}
//加头

-(void)populateTableHeader {
    my.tableHeaderView = ({
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 120.0f)] ;
        head =[[UIImageView alloc]init];
        head.image=[UIImage imageNamed:@"default_avatar_shadow.9.png"];
        
        head.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        head.frame = CGRectMake(0, 10, 80, 80);
        head.layer.masksToBounds = YES;
        head.layer.cornerRadius = 40.0;
        head.layer.borderColor = [UIColor whiteColor].CGColor;
        head.layer.borderWidth = 3.0f;
        head.layer.rasterizationScale = [UIScreen mainScreen].scale;
        head.layer.shouldRasterize = YES;
        head.clipsToBounds = YES;
        [view addSubview:head];
        
        head.userInteractionEnabled = YES;
        UITapGestureRecognizer *singleTap =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onUserFaceIvClick)];
        [singleTap setNumberOfTapsRequired:1];
        [head addGestureRecognizer:singleTap];
        
        int ypos = 120;
        int screenWidth = self.view.frame.size.width - 2 * 6;
        UILabel * nameLb = [[UILabel alloc] initWithFrame:CGRectMake(85, 30, 200, 200)];
        nameLb.text = @"逗比";
        nameLb.font = [UIFont systemFontOfSize:15];
        nameLb.textAlignment = NSTextAlignmentCenter;
        [view addSubview:nameLb];
        nameLb.userInteractionEnabled = YES;
        singleTap =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onUserNameClick)];
        [singleTap setNumberOfTapsRequired:1];
        [nameLb addGestureRecognizer:singleTap];
        ypos += 6 + 10;
        ypos += 40;
        CGRect frame = view.frame;
        frame.size.height = ypos;
        view.frame = frame;
        view;
    });
}


//加分割线
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 520, 30)];
        view.backgroundColor = [UIColor grayColor];
        return view;

}

//分割线宽度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 12;
}


//每个分组有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(TB_SECTION_COLLECT == section) {
        return titles.count;
    }
    else {
        return 1;
    }


}
//相当于getview
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"Cell";
    
    MyCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[MyCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if(indexPath.section < TB_SECTION_QUIT) {
        [cell populate:[[self.pic objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] title:[self.titles[indexPath.section] objectAtIndex:indexPath.row] height:44 style:MCC_STYLE_NORMAL];
        if (indexPath.section == TB_SECTION_COLLECT) {
            UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0,44, 400, 1)];
            footView.backgroundColor =[UIColor grayColor];
            [cell addSubview:footView];
        }
        
      
    }
    
    return cell;
}
//一共有多少行
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return totalSectionCount;


}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

下面是自定义的cell,这个就是item,这苹果的算坐标给我算迷糊的,都不知道哪是哪来,话说苹果手机的屏幕是多大的啊,我都不知道,谁让我不是富二代哪,也买不起苹果手机,你说整个Mac咋买的啊,这个应该从85年就开始攥钱,得从这说起,那是一个非常黑,非常静的夜晚,就说月黑风高,我翻墙闯进了李寡妇家,抢了2元钱,这为我买Mac注资了第一桶金,哎,也不知道现在李寡妇现在什么样了,好怀念跟隔壁老王一起玩耍的日子啊!如果我人生要是能重来,我就学文,我就当官去,贪污,出国,然后被遣送回国,哈哈,不扯来,贴代码了!


</pre><pre name="code" class="objc">//  MyCellTableViewCell.m
//  MyUItableView
//
//  Created by xiaoyuan on 15/4/24.
//  Copyright (c) 2015年 xiaoyuan. All rights reserved.
//

#import "MyCellTableViewCell.h"
@interface MyCellTableViewCell(){



}

@property (retain,nonatomic) UILabel *title;
@property(retain,nonatomic) UILabel *back;
@end

@implementation MyCellTableViewCell
@synthesize title;
@synthesize back;
@synthesize icon;
static int iconsize = 24;

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{


    self  = [super  initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if(self){
    
        icon  =[[UIImageView alloc] initWithFrame:CGRectMake(6 , (54-iconsize)/2, iconsize, iconsize)];
        
        [self.contentView addSubview:icon];
        title =[[UILabel alloc] initWithFrame:CGRectMake(6*2+iconsize, 22, 1000,44)];
        title.font =[UIFont systemFontOfSize:15];
        [self.contentView addSubview:title];
    
    }



    return self;

}

-(void)populate:(NSString *)iocn title:(NSString *)title height:(int)height style:(int)styles{

    self.icon.hidden = NO;
    self.title.hidden = NO;
    CGRect frame = self.title.frame;
    frame.origin.y = (height-40) / 2;
    self.title.frame = frame;
    
    self.title.text = title;


}


@end

//
//  MyCellTableViewCell.h
//  MyUItableView
//
//  Created by xiaoyuan on 15/4/24.
//  Copyright (c) 2015年 xiaoyuan. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MyCellTableViewCell : UITableViewCell

#define MCC_STYLE_NORMAL 0
#define MCC_STYLE_QUIT_BTN 100

@property(strong,nonatomic) UIImageView *icon;
//定义方法
-(void ) populate:(NSString*) iocn title:(NSString*) title height:(int) height style:(int) styles;
@end

哈哈,应该是写完了,不说了,这么晚了,我开始装逼去了,扯淡结束,装逼去了!

大神勿喷!

源码下载地址




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。