iOS使用数组添加到UITableView

Posted

技术标签:

【中文标题】iOS使用数组添加到UITableView【英文标题】:iOS Adding to UITableView using array 【发布时间】:2011-03-08 22:21:40 【问题描述】:

好的,我正在尝试向我的 tableview 添加一行。我一定做错了什么。我要添加到数组中,是否缺少使用新数组刷新 UITableView 的函数?

#import "RootViewController.h"
#import "DetailViewController.h"
@implementation RootViewController
@synthesize toolbar;
@synthesize window;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad 
    //theArray = [[NSArray alloc] initWithObjects:@"Apple",nil];
    theArray = [[NSMutableArray alloc] init];


    [super viewDidLoad];

    [window addSubview:[navigationController view]];
    [window addSubview:toolbar];
    [window makeKeyAndVisible];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;




#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    NSString *selectedTask = [theArray objectAtIndex:indexPath.row];
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
    dvController.selectedTask = selectedTask;
    [self.navigationController pushViewController:dvController animated:YES];
    [dvController release];
    dvController = nil;





- (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 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;



- (void)dealloc 
    [super dealloc];



@end

非常感谢您的帮助,因为我环顾了该站点并看到了有关将数组推送到 tableview 的一些内容,但进一步的研究使我陷入了一场愚蠢的追逐。

【问题讨论】:

【参考方案1】:

假设您已正确定义了所有 UITableViewDataSource 方法,您可能只需要在 UITableView 上调用 reloadData。有关 reloadData 方法的详细信息,请参阅UITableView 参考。

【讨论】:

以上是关于iOS使用数组添加到UITableView的主要内容,如果未能解决你的问题,请参考以下文章

React/Redux - 使用扩展运算符将对象添加到数组的开头

使用循环将值从 C++ 中的一维数组添加到二维数组中

在循环内将数组添加到多维数组

如何使用 if 条件将 django 数组元素添加到 js 数组中

React 使用 .push 通过 onClick 添加到数组

将数组添加到组合框