사용자 정의 클래스 (예 : DataItem
)를 만드는 oop 스타일을 활용하고 표시 할 배열을 DataItem
요소로 초기화 할 수 있습니다. 즉, name 및 image 요소가 포함 된 모델을 만들 수 있습니다. 이 같은 항목을 초기화하고 다음과 같이 (NSMutableArray
이 더 좋을 수) 배열에 추가 할 수 있습니다
지금
//.h
@interface DataItem : NSObject
{
NSString* name;
NSString* thunbmail;
}
@property (nonatomic, copy) NSString* name;
@property (nonatomic, copy) NSString* thunbmail;
- (id)initWithName:(NSString*)dName withThunbmail:(NSString*)dThunbmail;
@end
//.m
@implementation DataItem
@synthesize name;
@synthesize thunbmail;
- (id)initWithName:(NSString*)dName withThunbmail:(NSString*)dThunbmail
{
if(self = [super init])
{
name = [dName copy]; // release in dealloc!!
thunbmail = [dThunbmail copy]; // release in dealloc!!
}
return self;
}
// create dealloc here
@end
: 다음
DataItem* di = [[DataItem alloc] initWithName:@"name" withThunbmail:@"image.png"];
NSMutableArray* arrData = [[NSMutableArray alloc] init];
[arrData addObject:di];
// add other object here
self.tableData = arrData;
// release memory...
및 예를 들어
in cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell..
DataItem* di = (DataItem*)[self.tableData objectAtIndex:[indexPath row]];
cell.textLabel.text = di.name;
cell.imageView.image = [UIImage imageNamed:di.thunbmail];
return cell;
}
이것은 cont를 묶는 우아한 방법입니다. 하나의 클래스 모델에서.
희망이 있습니다.
P. 코드를 확인하십시오. 나는 손으로 썼다.
UITableViewDataSource 프로토콜의 일부인 cellForRowAtIndexPath에 개별 UITableViewCell 인스턴스를 만듭니다. – Rayfleck