Reputation: 1206
This works perfect if I return just 1 row of cells. But if I put more than 1 I get the error message: "index 1 beyond bounds [0 .. 0]"
I have made a xib file that has one table cell with a unique design and I just want to use that over and over only changing some labels. Am I going about this the wrong way?
- (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)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Cell";
SectionCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"ButtonCell" owner:self options:nil];
cell = [[SectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cell = (SectionCell *)[nib objectAtIndex:0];
}
cell.backgroundView.layer.masksToBounds = YES;
cell.backgroundView.layer.cornerRadius = 20.0;
cell.name.text=@"Cell Test";
tableView.backgroundView = nil;
tableView.backgroundColor = [UIColor clearColor];
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;
}
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 78;
}
Upvotes: 0
Views: 174
Reputation: 1206
Found the answer. I had to not use static for the table view and use dynamic prototype. The only downside is I couldn't use the custom xib cell and had to modify the default cell to look as close as possible to my static cell.
Upvotes: 0
Reputation: 9030
Change to this code snipped:
if (cell == nil)
cell = [[[SectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier] autorelease];
and remove your NSBundle logic as it is no longer necessary since you are instantiating the cell object yourself.
Example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Cell";
SectionCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
cell = [[[SectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier ] autorelease];
cell.backgroundView.layer.masksToBounds = YES;
cell.backgroundView.layer.cornerRadius = 20.0;
cell.name.text=@"Cell Test";
tableView.backgroundView = nil;
tableView.backgroundColor = [UIColor clearColor];
return cell;
}
Tip:
It isn't necessary to continuously set your tableview backgrounds the way you do. You can set it when your object is first loaded.
Upvotes: 1
Reputation: 2775
Instead of using :
cell = [nib objectAtIndex:indexPath.row];
use:
if (cell == nil) {
NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"ButtonCell" owner:self options:nil];
cell = (SectionCell *)[nib objectAtIndex:0];
}
Because your nib probably has only one object i.e your custom table view cell.
PS: Assuming you have a xib named "ButtonCell" having a single custom view with its custom class set to "SectionCell"
Upvotes: 1