以编程方式将单元格添加到UITableView

我刚刚开始为iPhone编程,我正在创建一个连接到数据库并获取一组行名称并显示它们的应用程序。 选择后,行背景颜色会发生变化,即您可以进行多项选择,它们都将是不同的颜色。 所以我从服务器返回XML没问题,我创建了一个UITableView来显示单元格。但是,我不知道如何将单元格添加到表格中。 我看了一下insertRowsAtIndexPaths但我不确定如何使用它? 据我了解, insertRowsAtIndexPaths有两个参数:

一个NSArray,它包含单元格所在的行以及在哪个部分。 这个问题是我的应用程序将有一个动态的行数。 如果我不知道我将拥有多少行,我将如何创建NSArray? 我可以使用NSMutableArray吗?

它需要的第二个参数是动画 – 这非常简单。

我遇到的另一个问题是我在哪里创建单元格? 你如何将细胞传递到tableview?

我试过阅读文档,但它似乎不太清楚! 这是我在视图控制器的loadview方法中的代码:

  //Before this I get the XML from the server so I am ready to populate //cells and add them to the table view NSArray *cells = [NSArray arrayWithObjects: [NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0], [NSIndexPath indexPathForRow:2 inSection:0], [NSIndexPath indexPathForRow:3 inSection:0], [NSIndexPath indexPathForRow:4 inSection:0], [NSIndexPath indexPathForRow:5 inSection:0], [NSIndexPath indexPathForRow:6 inSection:0], [NSIndexPath indexPathForRow:7 inSection:0], [NSIndexPath indexPathForRow:8 inSection:0], [NSIndexPath indexPathForRow:9 inSection:0], [NSIndexPath indexPathForRow:10 inSection:0], [NSIndexPath indexPathForRow:11 inSection:0], [NSIndexPath indexPathForRow:12 inSection:0], nil]; [eventTypesTable beginUpdates]; [eventTypesTable insertRowsAtIndexPaths:cells withRowAnimation:UITableViewRowAnimationNone]; [eventTypesTable endUpdates]; 

我认为你是从错误的方向接近这个。 UITableViews无法正常工作。 insertRowsAtIndexPaths用于将新行插入表中,而不是用于在第一个实例中填充它。

UITableViews通过调用许多委托方法来工作,这些方法允许您根据需要将数据呈现给表视图。 然后,框架负责填充单元格,处理滚动和触摸事件等。

我建议你先阅读这篇教程: http : //www.iosdevnotes.com/2011/10/uitableview-tutorial/ ,这对我来说相当透彻。 它解释了如何为表设置数据源以及如何配置UITableView显示数据的方式。

祝你好运!

不需要使用insertRowsAtIndexPaths

检查: UITableViewDataSource协议参考和UITableView类参考

魔法发生在这三种方法之间(UITableViewDataSource协议方法):

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

你只需要填充一个数组。 是的,它可以是NSMutableArray

您可以在- (void)viewDidLoad填充数组,例如:

 yourItemsArray = [[NSMutableArray alloc] initWithObjects:@"item 01", @"item 02", @"item 03", nil]; 

他们使用这样的数据源方法:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. // If You have only one(1) section, return 1, otherwise you must handle sections return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [yourItemsArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell... cell.textLabel.text = [NSString stringWithFormat:[yourItemsArray objectAtIndex:indexPath.row]]; return cell; } 

像这样,将自动创建单元格。

如果你挑战数组,只需要调用:

 [self.tableView reloadData]; 
 //######## Adding new section programmatically to UITableView ############ @interface MyViewController : UIViewController { IBOutlet UITableView *tblView; int noOfSection; } -(IBAction)switchStateChanged:(id)sender; @end @implementation MyViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad{ [super viewDidLoad]; noOfSection = 2; } - (void)viewDidUnload{ [super viewDidUnload]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { return YES; } return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } #pragma mark - TableView Delegate Methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return noOfSection; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 1; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.section == 2){ return 200; } return 50; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; UISwitch *switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 20, 10)]; cell.accessoryView = switchBtn; [switchBtn addTarget:self action:@selector(switchStateChanged:) forControlEvents:UIControlEventValueChanged]; cell.textLabel.font = [UIFont systemFontOfSize:14]; cell.detailTextLabel.font = [UIFont systemFontOfSize:11]; cell.textLabel.numberOfLines = 2; cell.detailTextLabel.numberOfLines = 2; } if(indexPath.section == 0){ cell.textLabel.text = @"Cell-1 Text"; cell.detailTextLabel.text = @"Cell-1 Detail text"; } else if(indexPath.section == 1){ cell.textLabel.text = @"Cell-2 Text"; } else { // new added section code is here... cell.textLabel.text = @"New Added section"; } [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; return cell; } -(IBAction)switchStateChanged:(id)sender{ UISwitch *switchState = sender; if(switchState.isOn == YES){ NSLog(@"ON"); NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2]; [self insertNewSectionWithIndexPath:indexPath]; } else { NSLog(@"OFF"); [self removeSectionWithIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]]; } } -(void)insertNewSectionWithIndexPath:(NSIndexPath *)indexPath{ noOfSection = 3; [tblView insertSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationFade]; } -(void)removeSectionWithIndexPath:(NSIndexPath *)indexPath{ noOfSection = 2; [tblView deleteSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationFade]; } @end 

你不必担心它。 将自动创建单元格。 只需看看这些UITableview类参考

Tableview_iPhone

您必须实现UITableView dataSource和委托协议。 另请参阅本教程UITableview教程

Interesting Posts