iPhone: Problem with tableview
This code doesnt work as required and doesnt load full data for the first time and works fine from the next time once scrolled.
#define ROW_HEIGHT 110
// Customize the a开发者_开发百科ppearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog( @"Setting table text." );
static NSString *CellIdentifier = @"Transaction";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
[cell addSubview:[[UILabel alloc] initWithFrame:CGRectMake(40.0, 0.0, 280.0, ROW_HEIGHT - 1)]];
}
NSUInteger row = [indexPath row];
NSLog( @"Table cell text: %@", [[transactionHistory objectAtIndex:row] description] );
UILabel *labelText = [[cell subviews] lastObject];
labelText.text = [[transactionHistory objectAtIndex:row] description];
labelText.font = [UIFont systemFontOfSize:14];
labelText.lineBreakMode = UILineBreakModeWordWrap;
labelText.numberOfLines = 5;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return ROW_HEIGHT;
}
What could be wrong?
replace this line
[cell addSubview:[[UILabel alloc] initWithFrame:CGRectMake(40.0, 0.0, 280.0, ROW_HEIGHT - 1)]];
To
[cell.contentView addSubview:[[UILabel alloc] initWithFrame:CGRectMake(40.0, 0.0, 280.0, ROW_HEIGHT - 1)]];
精彩评论