需要帮助将数组添加到tableview

时间:2011-05-09 22:33:25

标签: objective-c xcode

我无法将具有不同内容的单元格添加到我的应用中。我知道(或者认为)这需要使用数组完成,但我是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

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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;

如果我错了,请纠正我......

据我了解你的问题,你要做的是创建标签并以编程方式或通过Interface 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;
}

同样,这是一种非常简单的方法,您可以将内容添加到tableview ...

答案 2 :(得分:0)

您可以创建多个不同的UITableViewCell或您自己的自定义单元类,并将其添加到数组中,稍后在以下位置显示:

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

}

有了这个,你可以有几个不同的自定义单元格(我的意思是不同的是完全不同的单元格,例如带图像的第一个单元格而另一个没有图像)。

另一种选择是在

中进行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

如使用开关盒所示。无论哪种方式,都是关于偏好的。

我建议你先了解一下类的基本用法,比如NSArray,NSMutableArray和编写Objective-C语言。虽然它对学习开发应用程序没有多大帮助,但它将加快Cocoa环境中构建程序实际实现过程中的学习曲线。