ios UITableview操作

初学ios以及其UITableview控件,使用自定义cell和模型。

自定义模型:
DataModel.h

#ifndef DataModel_h
#define DataModel_h


#import <UIKit/UIKit.h>

@interface DataModel : UIViewController
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *dept;
@property (strong, nonatomic) NSString *image;


- (NSString *)getName;
- (NSString *)getDept;
- (NSString *)getImage;
@end



#endif /* DataModel_h */

DataModel.m

#import <Foundation/Foundation.h>
#import "DataModel.h"


@interface DataModel ()
    
@end

@implementation DataModel
@synthesize name = _name;
@synthesize dept = _dept;
@synthesize image = _image;



- (NSString *)getName{
    return _name;
}

- (NSString *)getDept{
    return _dept;
}

- (NSString *)getImage{
    return _image;
}

@end



自定义cell:
MyCell.h

#ifndef MyCell_h
#define MyCell_h

#import <UIKit/UIKit.h>
#import "DataModel.h"

@interface MyCell : UITableViewCell

@property(nonatomic,strong)UIView* achievementInfoView;//成果信息view
@property (nonatomic) CGFloat height;//cell的高度
@property(nonatomic,strong)DataModel*dataModel;//自定义数据model

-(void) setCellWithInfo:(DataModel*)dataModel indexPath:(NSIndexPath*)indexPath;

@end


#endif /* MyCell_h */

MyCell.m

#import <Foundation/Foundation.h>
#import "MyCell.h"
#import "UIImageView+WebCache.h"
#import "UIView+XMGExtension.h"

@interface MyCell ()
    
@end

@implementation MyCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews
{
    self.selectionStyle =UITableViewCellSelectionStyleNone;//选中后不变色

    self.achievementInfoView = [[UIView alloc] initWithFrame:CGRectZero];//CGRectMake(0,0,0,0)
    [self.contentView addSubview:self.achievementInfoView];

    self.height = 0;

}


-(void) setCellWithInfo:(DataModel*)dataModel indexPath:(NSIndexPath*)indexPath
{
    for(UIView *view in [self.achievementInfoView subviews])
    {
        [view removeFromSuperview];
    }
    self.achievementInfoView.frame = CGRectMake(0, 0, 100, 0);
    
    self.dataModel = dataModel;
    
    
    //头像
    UIImageView *paperImageview = [[UIImageView alloc]initWithFrame:CGRectMake(24, 14, 60, 60)];
    //设置占位图
    [paperImageview sd_setImageWithURL:[NSURL URLWithString:dataModel.image] placeholderImage:[UIImage imageNamed:@"holderPNG"] options:SDWebImageRetryFailed];
    paperImageview.layer.cornerRadius = 30;
    paperImageview.clipsToBounds = YES;
    
    [self.achievementInfoView addSubview:paperImageview];
    //给图片贴上按钮
    paperImageview.userInteractionEnabled = YES;
    UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    imageBtn.frame = paperImageview.bounds;
    imageBtn.backgroundColor = [UIColor clearColor];
    [imageBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [paperImageview addSubview:imageBtn];
    
    //作者
    UILabel *authorLab = [[UILabel alloc]initWithFrame:CGRectMake(24+60+10, paperImageview.y, 200, 30)];
    authorLab.font = [UIFont boldSystemFontOfSize:20];
    authorLab.text = dataModel.name;
    [self.achievementInfoView addSubview:authorLab];

    //单位
    UILabel *deptLab = [[UILabel alloc]initWithFrame:CGRectMake(24+60+10, paperImageview.y+30, 300, 20)];
    deptLab.textColor = [UIColor grayColor];
    deptLab.text = dataModel.dept;
    [self.achievementInfoView addSubview:deptLab];
    
    //添加按钮
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.frame = CGRectMake(24+60+5+200, paperImageview.y+10, 100, 30);
    [addBtn setTitle:@"添加" forState:UIControlStateNormal];
    [addBtn setTitleColor:UIColor.systemBlueColor forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //[self.achievementInfoView addSubview:addBtn];
    [self.contentView addSubview:addBtn];
    
    self.achievementInfoView.frame = CGRectMake(0, 0, 100, paperImageview.height);
    self.height = self.achievementInfoView.height+24;//设置当前cell高度
}


// 添加按钮点击事件
-(void)addBtnClick:(UIButton *)button{
    NSLog(@"点击了cell上的添加按钮");
}



ViewController.h

#import <UIKit/UIKit.h>



@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;        //用来装载自定义类DataModel
@end

ViewController.m

#import "ViewController.h"
#import "DataModel.h"
#import "MyCell.h"
#import "NewViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *array = [[NSArray alloc] initWithObjects:@"何", @"陈",
                      @"韦", @"马", @"李", @"高", @"刘",
                      @"攸", @"xiang", nil];
    NSArray *array2 = [[NSArray alloc] initWithObjects:@"研究所", @"研究所", @"软件(深圳)有限公司",@"软件(深圳)有限公司", @"化工大学",                  @"化工大学",@"民族大学", @"师范大学",@"研究所", nil];
    NSArray *array3 = [[NSArray alloc] initWithObjects:@"https://www.123456.com/avatars/a4/1c/ea/xxxx.png?A=DUnI",
                        @"https://www.123456.com/avatars/51/80/1b/xxxxx.jpg",
                        @"https://www1.123456.com/avatars/ee/b7/04/xxxxx.jpg",
                        @"https://www.123456.com/avatars/39/26/a9/xxxxxx.png?A=DqEJ",
                        @"https://www.123456.com/avatars/7e/be/c1/xxxxx.jpg",
                        @"https://www.123456.com/avatars/53/ea/43/xxxxx.png?A=DNYc",
                        @"https://www.123456.com/avatars/f8/e2/72/xxxxx.png?A=Dna3",
                        @"https://www.123456.com/avatars/73/6d/8b/xxxxxx.png?A=DaLS",
                        @"https://www.123456.com/avatars/8c/7c/9c/xxxxx.jpg", nil];
    
    //创建多个DataModel对象,并添加到数组当中
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for(int i=0; i<array.count; i++){
        DataModel *model = [[DataModel alloc] init];
        model.name = array[i];
        model.dept = array2[i];
        model.image = array3[i];

        [arr insertObject:model atIndex:i];
    }
    self.modelArray = arr;

    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

    _tableView.delegate = self;
    _tableView.dataSource = self;

    [self.view addSubview:_tableView];
}


#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    return [self.modelArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //说明:UITableViewCell表示表视图单元格,UITableView里的每一行都是UITableViewCell
    static NSString *cellIdentifier = @"SMPaperSearchTableCell";//定义一个可重用标识
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];//从重用队列里获取可重用的cell
    if (!cell)
    {
        //如果不在,创建一个可重用cell
        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellIdentifier];
    }
    
    if (indexPath.row < [self.modelArray count])
    {
        DataModel*dataModel = self.modelArray[indexPath.row];
        [cell setCellWithInfo:dataModel indexPath:indexPath];
    }
    
    return cell;
}



//设置每条cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 100;
    MyCell *cell = (MyCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    return cell.height;
}

//选中任意行触发
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *name = [self.modelArray[indexPath.row] getName];
    NSString *msg = [NSString stringWithFormat:@"已添加%@", name];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];

    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }]];

    [self presentViewController:alertController animated:true completion:nil];
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值