关闭

tableView自定义cell

标签: tableViewtableViewCell
15人阅读 评论(0) 收藏 举报
<pre name="code" class="objc">#import <UIKit/UIKit.h>
#import "cell.h"

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>


@end



#import "ViewController.h"
#define IDENTIFIER @"identifier"

@interface ViewController ()
@property (nonatomic) NSArray *dataList;
@property (nonatomic) BOOL nibsRegistered;

- (void)click:(id) sender;
@end

@implementation ViewController
@synthesize dataList;
@synthesize nibsRegistered;

- (void)viewDidLoad {
    dataList = [[NSArray alloc] initWithObjects:@"Line1",@"Line2",@"Line3", nil];
    [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.
}

- (void)click:(id)sender{
    UIButton *button = sender;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Tapped" message:[dataList objectAtIndex:button.tag] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!nibsRegistered){
        UINib *nib = [UINib nibWithNibName:@"cell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:IDENTIFIER];
        [self setNibsRegistered:YES];
    }
    cell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
    [cell.uiButton setTag:indexPath.row];
    [cell.uiButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [cell.uiLabel setText:[dataList objectAtIndex:indexPath.row]];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [dataList count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
@end

#import <UIKit/UIKit.h>

@interface cell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *uiLabel;
@property (weak, nonatomic) IBOutlet UIButton *uiButton;
@property (weak, nonatomic) IBOutlet UISwitch *uiSwitch;

@end

#import "cell.h"

@implementation cell

- (void)awakeFromNib {
    // Initialization code
}

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

    // Configure the view for the selected state
}

@end



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16次
    • 积分:2
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章存档
    阅读排行
    评论排行