Ø (9)UITAbleView
Add Custom cell in table using structure in iphone
ip14ViewController.h
#import <UIKit/UIKit.h>
@interface ip14ViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
NSArray *dataArr;
}
@end
ip14ViewController.m
#import "ip14ViewController.h"
#import "Student.h"
#import "MyuCustomTableCell.h"
- (void)viewDidLoad
{
[super
viewDidLoad];
Student *s0 =
[Student alloc];
s0.name =
@"Alan";
s0.age = 18;
Student *s1 =
[Student alloc];
s1.name =
@"Bob";
s1.age = 20;
dataArr =
[[NSArray alloc] initWithObjects:s0, s1, nil];
}
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return
dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString
*CellIdentifier = @"Cell";
UITableViewCell
*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell =
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
Student
*currentStdObj = [dataArr objectAtIndex:indexPath.row];
MyuCustomTableCell
*MyuCustomTableCellVC = [[MyuCustomTableCell alloc] init];
MyuCustomTableCellVC.myStudentObj = currentStdObj;
[cell.contentView
addSubview:MyuCustomTableCellVC.view];
return cell;
}
MyuCustomTableCell.h
#import <UIKit/UIKit.h>
#import "Student.h"
@interface MyuCustomTableCell : UIViewController
@property (nonatomic, retain) IBOutlet UILabel *nameLbl,
*ageLbl;
@property (nonatomic, retain) Student *myStudentObj;
@end
MyuCustomTableCell.m
@synthesize nameLbl, ageLbl;
@synthesize myStudentObj;
- (void)viewDidLoad
{
[super
viewDidLoad];
nameLbl.text =
[NSString stringWithFormat:@"Name: %@", myStudentObj.name];
ageLbl.text =
[NSString stringWithFormat:@"Age: %d", myStudentObj.age];;
}
Student.h
@property (nonatomic, retain) NSString *name;
@property (nonatomic, readwrite) int age;
Student.m
@synthesize name,
age;
No comments:
Post a Comment