Iphone iOS tableView帮助
我正在尝试创建一个允许我删除和添加项目的tableView。我遇到的最大问题是我的所有细胞都无法显示。我不确定我漏掉了什么。这是TableViewController类Iphone iOS tableView帮助,iphone,xcode,ios,ipad,tableview,Iphone,Xcode,Ios,Ipad,Tableview,我正在尝试创建一个允许我删除和添加项目的tableView。我遇到的最大问题是我的所有细胞都无法显示。我不确定我漏掉了什么。这是TableViewController类 #import "GroceryListTableViewController.h" @implementation GroceryListTableViewController @synthesize groceries; /*- (NSMutableArray *) groceries { if(grocer
#import "GroceryListTableViewController.h"
@implementation GroceryListTableViewController
@synthesize groceries;
/*- (NSMutableArray *) groceries
{
if(groceries == nil)
[self addToGroceries:@"Groceries"];
return groceries;
}*/
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
self.groceries = [NSMutableArray arrayWithObjects:@"Steak", @"Juice", @"Stuff", nil];
if (self) {
}
return self;
}
-(void) addToGroceries:(NSString *)grocery
{
[self.groceries addObject:grocery];
}
- (void)dealloc
{
[groceries release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (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];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.groceries count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"GroceryListCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [self.groceries objectAtIndex:indexPath.section];
// 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;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#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];
*/
}
@end
谢谢你的帮助。我认为问题在于我在列表中填充了项目,但我尝试了其他方法,例如构造函数,但它们也不起作用。
我对代码进行了编辑,以使其成为正确的解决方案,因此其他问题能够帮助您尝试这一点……尽管代码的其余部分可能需要一些审阅
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
不,这是一种可选方法。如果没有,默认情况下它将返回1。正确。如果存在,则应返回1或实际节数。它可能是其他开发的占位符。lipd可以返回1并检查或完全删除它,或者如果tableview需要额外的复杂性,则将其保留为占位符。是的,我完全忘记了删除此方法,因此它现在会在列表中显示项目,但由于某些原因,只有我的NSMutableArray中的第一个项目显示为数组大小的3倍。我想这只是一些逻辑错误,因为我正试图遵循关于TableView的教程,可能只是弄糟了一些东西,因为我所做的略有不同;谢谢你,乔丹!我仍在尝试适应所有语法。假设groceries是一个retain属性,则会出现内存泄漏,因为您也在手动调用retain。感谢您的提示,因为我实际上使用的是一本书中的方法,甚至没有注意到我导致了泄漏。