Need help adding an array to a table view

I am having problems adding cells with different content to my application. I know (or think) that this needs to be done with an array, but I'm new to xcode and objective-c, so I'm not quite sure how to do this. Any help is much appreciated. Thank.

Here is a copy of my current code:

#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
+3
source share
3 answers

First, note that cellForRowAtIndexPath is called several times - once for each cell. I think your best option is to declare an array and populate it with the objects you want to display as follows:

- (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;
}
+2

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;

, ...

, , Builder ( , ), , ..

, .

// 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.

0

All Articles