哦,自定义UITableViewCell就算完成了,那么就让我们看看是怎么把他加到UITableView上的。
.m文件
#import "MyUITableViewYourSelfCellControl.h" @implementation MyUITableViewYourSelfCellControl @synthesize MyUITableView; @synthesize MyTableViewCell; @synthesize Mypeople; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { //定义数据 NSDictionary *dic1=[[NSDictionary alloc] initWithObjectsAndKeys:@"dong",@"name",@"22",@"age", nil]; NSDictionary *dic2=[[NSDictionary alloc] initWithObjectsAndKeys:@"wang",@"name",@"23",@"age", nil]; NSDictionary *dic3=[[NSDictionary alloc] initWithObjectsAndKeys:@"zhang",@"name",@"24",@"age", nil]; NSDictionary *dic4=[[NSDictionary alloc] initWithObjectsAndKeys:@"li",@"name",@"25",@"age", nil]; NSDictionary *dic5=[[NSDictionary alloc] initWithObjectsAndKeys:@"sui",@"name",@"26",@"age", nil]; NSArray *people=[NSArray arrayWithObjects:dic1,dic2,dic3,dic4,dic5, nil]; self.Mypeople=people; [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)viewDidUnload { [self setMyUITableView:nil]; [self setMyTableViewCell:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.Mypeople count]; } -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSInteger row=[indexPath row]; static NSString *[email protected]"dong"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:Tagtittle]; if (cell==nil) { //最关键的就是这句。加载自己定义的nib文件 NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"Empty" owner:self options:nil]; //此时nib里含有的是组件个数 if ([nib count]>0) { cell=self.MyTableViewCell; } } NSDictionary *dic=[self.Mypeople objectAtIndex:row]; NSString *name=[dic objectForKey:@"name"]; NSString *age=[dic objectForKey:@"age"]; //通过设定的tag值来找对应的lable,给其复制。 UILabel *lablename=(UILabel*)[cell viewWithTag:1]; lablename.text=name; UILabel *lableage=(UILabel*)[cell viewWithTag:2]; lableage.text=age; return cell; } @end相关文章: