UITableView定製cell(不能複用),複用需建立TableViewCell程序員
ViewController.h // UITableView定製cell // // Created by on 16/7/7. // Copyright © 2016年 gg. All rights reserved. //ide
#import <UIKit/UIKit.h> #import "TableViewCell.h" @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (weak, nonatomic) IBOutlet UIImageView *imageView; @property (weak, nonatomic) IBOutlet UILabel *label; @property (weak, nonatomic) IBOutlet UIButton *but1; @property (weak, nonatomic) IBOutlet UITextField *textField; @property (weak, nonatomic) IBOutlet UIButton *but2;函數
@endatom
// TableViewCell.m // UITableView定製cell // // Created by 非凡程序員 on 16/7/7. // Copyright © 2016年 gg. All rights reserved. //.net
#import "TableViewCell.h"code
@implementation TableViewCell //構造函數 -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { NSArray *array=[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil]; self=array[0]; } return self; }orm
(void)awakeFromNib { // Initialization code }get
(void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated];it
// Configure the view for the selected state }io
@end
ViewController.m // UITableView定製cell // // Created by 非凡程序員 on 16/7/7. // Copyright © 2016年 gg. All rights reserved. //
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 200; }
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // NSArray *array=[[NSBundle mainBundle]loadNibNamed:@"cell" owner:self options:nil]; // UITableViewCell *cell=array[0]; // _imageView.image=[UIImage imageNamed:@"韓非01.jpg"]; // _label.text=@"你猜"; // [_but1 setTitle:@"+" forState:UIControlStateNormal]; // [_but1 addTarget:self action:@selector(button1:) forControlEvents:UIControlEventTouchUpInside]; // _textField.text=@"1"; // _textField.enabled=NO; // [_but2 setTitle:@"-" forState:UIControlStateNormal]; // cell.accessoryType=UITableViewCellAccessoryDetailButton;
static NSString *reuse=@"cell"; UITableViewCell *cell1=[tableView dequeueReusableCellWithIdentifier:reuse]; if (cell1==nil) { NSLog(@"======"); cell1=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]; } //Checkmark: //cell1.accessoryType=UITableViewCellAccessoryCheckmark; //DetailButton: //cell1.accessoryType=UITableViewCellAccessoryDetailButton; //DetailDisclosureButton: cell1.accessoryType=UITableViewCellAccessoryDetailDisclosureButton; //DisclosureIndicator:> cell1.accessoryType=UITableViewCellAccessoryDisclosureIndicator; cell1.imageView.image=[UIImage imageNamed:@"韓非01.jpg"]; return cell1;
} -(void)button1:(UIButton *)button { NSLog(@"======"); }
@end