2011-05-09 4 views
3

내 응용 프로그램에 다른 내용의 셀을 추가하는 데 문제가 있습니다. 배열을 사용하여이 작업을 수행해야한다고 생각합니다. 그러나 xcode와 objective-c에 익숙하지 않으므로이 작업을 수행하는 방법을 정확히 알지 못합니다. 어떤 도움이라도 대단히 감사합니다. 감사.배열을 테이블 뷰에 추가하는 데 도움이 필요합니다.

#import "RootViewController.h" 
#import "CustomCell.h" 

@implementation RootViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations. 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 5; 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 100; 
} 
// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    cell.imageViews.image = [UIImage imageNamed:@"captainaq_4.jpg"]; 
    cell.firstLabel.text = @"name "; 
    cell.secondLabel.text = @"second name"; 
    cell.thirdLabel.text = @"third name"; 
    cell.thirdLabel.textAlignment = UITextAlignmentCenter; 
    // Configure the cell. 
    return cell; 
} 

/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 

/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     // Delete the row from the data source. 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) 
    { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 
*/ 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    [detailViewController release]; 
    */ 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 

답변

0

확인 ... 지금까지 내가 본대로, 나는 다음과 같은 유효 같아요 : 여기

내 현재 코드의 복사본입니다.

cell.imageViews.image = [UIImage imageNamed:@"captainaq_4.jpg"]; 
cell.firstLabel.text = @"name "; 
cell.secondLabel.text = @"second name"; 
cell.thirdLabel.text = @"third name"; 
cell.thirdLabel.textAlignment = UITextAlignmentCenter; 

만약 내가 잘못 지금까지 내가 귀하의 질문에, 당신이 할 수있어하면 해당 레이블을 만들고 프로그래밍 방식 또는 쉽게 중 인터페이스 빌더 (를 통해 추가 인 이해로 ...

날 수정 여기

// Customize the appearance of table view cells. 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // adding indicator to the rows 

} 

// Configure the cell... 

switch (indexPath.row) { 
    case 0: 
     cell.textLabel.text = @"John Doe"; 
     cell.detailTextLabel.text = @"DEPT"; 
     //cell.imageView.image = [UIImage imageNamed:@"meeting_color.png"]; 
     break; 
    case 1: 
     cell.textLabel.text = @"Mary Smith"; 
     cell.detailTextLabel.text = @"DEPT"; 
     //cell.myImageView.image = [UIImage imageNamed:@"call_color.png"]; 
     break; 
    case 2: 
     cell.textLabel.text = @"Bob Wong"; 
     cell.detailTextLabel.text = @"DEPT"; 
     //cell.myImageView.image = [UIImage imageNamed:@"calendar_color.png"]; 
     break; 
    default: 
     break; 
} 

return cell; 
} 

다시 당신이 할 수있는 일의 예 ..입니다 ... 당신이 셀에 원하는 내용을 표시 할 수 있도록) 당신을 위해

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

} 

으로 : 아주 간단한 당신이있는 tableview에 내용을 추가 할 수있는 방법 ...

0

당신은 여러 가지있는 UITableViewCell 또는 자신의 사용자 정의 셀 클래스를 만들고 배열에 추가하고 아래에 나중에 표시 할 수있다 이, 당신은 몇 가지 별개의 사용자 정의 셀을 가질 수 있습니다 (다른 하나는 이미지가없는 반면 다른 셀은 이미지가있는 첫 번째 셀과는 완전히 다른 셀입니다).

다른 대안은 스위치 케이스를 이용하여 도시 된 바와 같이

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

내부를 수행된다. 어느 쪽이든 그것은 선호도에 관한 것입니다.

NSArray, NSMutableArray 및 Objective-C 언어 작성과 같은 클래스의 기본 사용법에 대해 더 자세히 배우는 것이 좋습니다. 개발중인 앱을 배우는 데 많은 도움이되지는 않지만 코코아 환경에서 프로그램을 실제로 구현하는 동안 학습 곡선을 빠르게 할 수 있습니다.

2

먼저 각 셀에 대해 cellForRowAtIndexPath가 여러 번 호출된다는 점에 유의하십시오.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *days[] = {@"Mon", @"Tues", @"Wed", @"Thurs", @"Fri", @"Sat", @"Sun"}; 
static NSString *CellIdentifier = @"Cell"; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
[[cell textLabel] setText:days[indexPath.row]]; 

return cell; 
} 

을 또는 당신이 그것을 내에서 개체를 조작해야하는 경우, 다른 곳에 배열을 채울 : 나는 당신의 최선의 선택은 다음과 같이 배열을 선언하고 표시 할 개체로 채우하는 것입니다 생각합니다. 또한 당신이 그것의 이미지와 헤더를 갖고 싶어 나타납니다

[[cell textLabel] setText:[yourArray objectAtIndex:indexPath.row]]; 

:처럼이 경우 (배열이 문자열로 채워집니다 가정)의 setText에 전화가 보일 것이다. 이 경우 다음을 사용하십시오.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 
    //create your UIImageView and add your image 
    return yourImageView; 
} 
관련 문제