I've spent the last 3 hours scratching my head at this issue because all seems correct, except I can't get through to my tableviewcell class to set the uilabels in my custom cell.
here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
//cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
//cell.imageViewSection =
// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length开发者_运维知识库] - 1];
NSString *textTitle = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
NSURL *imageurl = [NSURL URLWithString:[[stories objectAtIndex: storyIndex] objectForKey: @"description"]];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:imageurl]];
cell.imageViewSection = image;
cell.titleLabelText.text = textTitle;
return cell;
}
If someone can help me out it would be awesome :)
You should not rely on the cell always being returned as object at index 0 in the array. Do a simple loop to find the actual cell.
And add typecasts for the cell subclass you use
-(UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* cellID = @"cellID";
MyCell* cell = (id)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell"
owner:self
options:nil];
for (cell in nib) {
if ([cell isKindOfClass:[MyCell class]]) {
break;
}
}
}
// Safely do tuff to cell
return cell;
}
This snippet assumes that the cell is at least available, the behavior if no table view cell is returned is undefined.
You're assigning and casting TableViewCell to UITableViewCell variable and UITableViewCell does not have those outlets. Instead, you should do the following..
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
cell = (TableViewCell *)[nib objectAtIndex:0];
}
精彩评论