2011-05-09 79 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

OK了...就我所看到的,我不認爲以下是有效的。

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; 
} 

再次證明是一個很簡單的方法就可以將內容添加到實現代碼如下...

0

您可以創建多個不同的UITableViewCell或您自己的自定義單元格級,並把它添加到陣列,並在以後顯示它:

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

} 

隨着這個,你可以有幾個不同的自定義單元格(我的意思是不同的是完全不同的單元格,例如第一個單元格與圖像,而另一個沒有圖像)。

雖然另一種方法是做到這一點的

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

內部,如圖通過使用開關殼體。無論哪種方式,都是關於偏好。

我建議你先了解更多關於類的基本用法,比如NSArray,NSMutableArray和編寫Objective-C語言。雖然它對學習開發應用程序沒有多大幫助,但它會在Cocoa環境中實際構建程序的過程中加快學習速度。

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; 
} 

或者填充陣列別的地方,如果你需要在它操縱的對象。在這種情況下(假設你的數組填充字符串),你的電話的setText會是什麼樣子:

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

這似乎也想有它的圖像的頭。在這種情況下,請使用:

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