UITableViewController-简单自定义单元格

来源:互联网 发布:mac 桌面显示便签 编辑:程序博客网 时间:2024/06/13 17:44
////  MyUItableViewCell.h//  AppUI组件学习////  Created by 麦子 on 15/6/23.//  Copyright (c) 2015年 麦子. All rights reserved.//#import <UIKit/UIKit.h>typedef void (^ButCallBack) (id obj);@interface MyUItableViewCell : UITableViewCell@property(nonatomic,strong) ButCallBack butCallBack;@property(nonatomic,strong) UITextField *textField;@property(nonatomic,strong) UIButton *btn;@end
////  MyUItableViewCell.m//  AppUI组件学习////  Created by 麦子 on 15/6/23.//  Copyright (c) 2015年 麦子. All rights reserved.//#import "MyUItableViewCell.h"@implementation MyUItableViewCell@synthesize butCallBack;@synthesize textField;@synthesize btn;- (instancetype)initWithStyle:(UITableViewCellStyle)styleA reuseIdentifier:(NSString *)reuseIdentifierA{    self = [super initWithStyle:styleA reuseIdentifier:reuseIdentifierA];    if (self != nil) {        [self createView];    }    return self;}- (void)createView{    textField= [[UITextField alloc] init];    textField.frame = CGRectMake(5, 10, 80, 20);    [self addSubview:textField];        btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];    btn.frame = CGRectMake(100, 10, 100, 30);    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];    [self addSubview:btn];}- (void)btnClick:(UIButton *)btnEntity{      self.butCallBack(btnEntity); // 调用接口, 实现类自己去实现。}@end

////  MyTableViewController.m//  AppUI组件学习////  Created by 麦子 on 15/6/23.//  Copyright (c) 2015年 麦子. All rights reserved.//#import "MyTableViewController.h"#import "MyUItableViewCell.h"@implementation MyTableViewController@synthesize array;- (void)viewDidLoad {    [super viewDidLoad];    self.title = @"主菜单";    self.view.backgroundColor = [UIColor whiteColor];    [self createTableView];    self.tableView.delegate = self;    self.tableView.dataSource = self;}// 常用的table的属性- (void)createTableView{    array = [NSMutableArray arrayWithObjects:@"韩红",@"张国荣",@"刘德华",@"许巍", nil];}// 确定表视图有多少个区- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{        return 1;    }// 分区头- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    if (section == 0) {        return @"分区1号";    }    return @"分区2号";}// 分区尾- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{    if (section == 0) {        return @"分区尾部1号";    }    return @"分区尾部2号";}// 区里面有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{        return  self.array.count;}// 设置单元格- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //    // 查看内存中是否有//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];//    if (cell == nil) {//        // 样式决定你显示的形式//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];//    }//    //    cell.textLabel.text = self.array[indexPath.row];//    // 设置颜色//    cell.textLabel.textColor = [UIColor blueColor];//    // 对于显示的字体的大小和颜色,都是在这里进行设置的。//    cell.detailTextLabel.text = @"男歌星";//    cell.accessoryType = UITableViewCellAccessoryCheckmark;//    cell.imageView.image = [UIImage imageNamed:@"tupian5.jpg"];        // 自定义单元格    MyUItableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];    ButCallBack callback =  ^(id obj){        if ([obj isKindOfClass:[UIButton class]]) {            UIButton *btnObj = (UIButton *)obj;            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"点击了" message:@"我点" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"OK", nil];            [alert show];            NSLog(@"回调成功----%@-----",btnObj.titleLabel.text);        }    };        if (cell == nil) {        cell = [[MyUItableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];    }    [cell setButCallBack:callback];    cell.textField.text = self.array[indexPath.row];    NSString *btnMessage = self.array[indexPath.row];    btnMessage = [btnMessage stringByAppendingFormat:@"-按钮-%ld",indexPath.row];    [cell.btn setTitle:btnMessage forState:UIControlStateNormal];    return cell;}// 设置缩进-- 显示的文字缩进- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{       return  indexPath.row;}// 单元格的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{        return 80;}// 取消选中某一行- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{       NSLog(@"取消%ld",indexPath.row);}// 选中某一行- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{   NSLog(@"选中%ld",indexPath.row);}// 删除- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//    NSLog(@"删除触发");        // 数据删除    [array removeObjectAtIndex:indexPath.row];    // 界面删除    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];        }@end


0 0
原创粉丝点击