2012-02-07 3 views
1

다음 아이콘 이미지 (image1.png, image2.png 등)를 다음 UITableview 배열에 추가하고 싶습니다. 정말로 누군가 도움이 필요합니다. 미리 감사드립니다.아이콘 이미지를 UITableview 배열에 추가

{ 
self = [super init]; 
if (self) { 
    // Custom initialization 
    self.tableData = [NSArray arrayWithObjects:@"Region", @"Subregion", @"Country",  @"County", @"City", @"District", nil]; 

    CGRect frame = self.view.bounds; 
    frame.size.height -= 100; 
    self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped]; 
    [self.tableView setBackgroundColor:[UIColor clearColor]]; 
    [self.tableView setDataSource:self]; 
    [self.tableView setDelegate:self]; 
    [self.tableView setScrollEnabled:NO]; 

    [self.view addSubview:self.tableView]; 
} 
return self; 
} 
+1

UITableViewDataSource 프로토콜의 일부인 cellForRowAtIndexPath에 개별 UITableViewCell 인스턴스를 만듭니다. – Rayfleck

답변

4

사용 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. 
    cell.textLabel.text = @"cell text"; 
    cell.imageView.image = [UIImage imageNamed:@"image1.png"]; 
    return cell; 
} 
4
당신은 사진

self.tablePicture = [NSArray arrayWithObjects:@"pic1.png", @"pic2.png", @"Country.png", nil]; 
당신이 그 (것)들을 표시 할 순서대로

과 cellForRowAtIndexPath에서의 이름을 유지 다른 배열을 만들 수

단지 쓰기

cell.imageView.image = [UIImage imageNamed:[tablePicture objectAtIndex:indexPath.row]]; 
+0

위의 코드를 배열에 적용하는 방법은 무엇입니까? – CraigBellamy

+0

헤더 파일에 배열을 선언하고 tableData 배열처럼 합성해야합니다. – Radu

+0

헤더 파일에 배열을 어떻게 선언합니까? 바보 같은 소리지만 그걸 잘 못해서 미안 해요! – CraigBellamy

1

사용자 정의 클래스 (예 : 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. 코드를 확인하십시오. 나는 손으로 썼다.

+1

이 답변을 받아 들여야합니다 ... !! – Shailesh

+0

@shailesh thanks –