Hey everbody, i'm getting some trouble here with the UITableView.
I'm setting my UITableView to create the rows dynamically using a XML. On iPhone Simulator everything works fine, but when i just build it on device, when i drag the table to up or down, the app crashes.
Something what i realized is that app just crashes when some row get out of the screen. So, when the table still visible on screen, the app works fine, but when i drag it out of screen, crashes.
Here goes the code:
#import "ComentariosViewController.h"
#import "TBXML.h"
@implementation ComentariosViewController
@synthesize listaComentarios, tabelaComentarios, nomesComentarios, rateComentarios;
- (void)viewDidLoad
{
listaComentarios = [[NSMutableArray alloc] init];
nomesComentarios = [[NSMutableArray alloc] init];
rateComentarios = [[NSMutableArray alloc] init];
TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://192.168.0.101/dev/mcomm/produto.xml"]] retain];
TBXMLElement * rootXMLElement = tbxml.rootXMLElement;
TBXMLElement * comentarios = [TBXML childElementNamed:@"comentarios" parentElement:rootXMLElement];
TBXMLElement * comentario = [TBXML childElementNamed:@"comentario" parentElement:comentarios];
while (comentario) {
NSString * descText = [TBXML textForElement:comentario];
NSString * nome = [TBXML valueOfAttributeNamed:@"nome" forElement:comentario];
NSString * rate = [TBXML valueOfAttributeNamed:@"rate" forElement:comentario];
[listaComentarios addObject:descText];
[nomesComentarios addObject:nome];
[rateComentarios addObject:rate];
comentario = [TBXML nextSiblingNamed:@"comentario" searchFromElement:comentario];
}
tabelaComentarios.separatorStyle = UITableViewCellSeparatorStyleNone;
tabelaComentarios.rowHeight = 105;
tabelaComentarios.backgroundColor = [UIColor clearColor];
UIImageView *baloonTop =
[[[UIImageView alloc]
initWithFrame:
CGRectMake(165, 25, 43, 29)]
autorelease];
baloonTop.image = [UIImage imageNamed:@"ComentsBaloon.png"];
// Texto antes dos Comentarios
UIView *containerView =
[[[UIView alloc]
initWithFrame:CGRectMake(0, 0, 300, 70)]
autorelease];
UILabel *headerLabel =
[[[UILabel alloc]
initWithFrame:CGRectMake(0, 20, 300, 40)]
autorelease];
headerLabel.text = NSLocalizedString(@"Comentários", @"");
headerLabel.textColor = [UIColor grayColor];
headerLabel.shadowColor = [UIColor whiteColor];
headerLabel.shadowOffset = CGSizeMake(1, 0);
headerLabel.font = [UIFont boldSystemFontOfSize:26];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
[containerView addSubview:baloonTop];
self.tabelaComentarios.tableHeaderView = containerView;
[tbxml release];
}
// Numero de Secoes da Tabela (essecial)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Numero de Linhas da Table (dinamico)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return[listaComentarios count];
}
// Criacao e montagem da tabela
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
const NSInteger TOP_LABEL_TAG = 1001;
UILabel *topLabel;
UITextView *bottomLabel;
UIImageView *rateBase;
stat开发者_如何转开发ic NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//
// Create the cell.
//
cell =
[[[UITableViewCell alloc]
initWithFrame:CGRectMake(0, 0, 180, 200)
reuseIdentifier:CellIdentifier]
autorelease];
//
// Create the label for the top row of text
//
topLabel =
[[[UILabel alloc]
initWithFrame:
CGRectMake(10, 5, 200, 20)]
autorelease];
topLabel.font = [UIFont boldSystemFontOfSize:15];
topLabel.textColor = [UIColor grayColor];
[cell.contentView addSubview:topLabel];
// Rates
rateBase =
[[[UIImageView alloc]
initWithFrame:
CGRectMake(215, 10, 67, 10)]
autorelease];
NSString *rateValue = [rateComentarios objectAtIndex:indexPath.row];
NSString *rateImage = [[NSString alloc] initWithFormat:@"Rate%@.png",rateValue];
rateBase.image = [UIImage imageNamed:rateImage];
[cell.contentView addSubview:rateBase];
// Top Baloon
//
// Configure the properties for the text that are the same on every row
//
//
// Create the label for the top row of text
//
bottomLabel = [[[UITextView alloc] initWithFrame: CGRectMake(2, 28, 270, 58)] autorelease];
[cell.contentView addSubview:bottomLabel];
bottomLabel.editable = NO;
bottomLabel.scrollEnabled = NO;
//
// Configure the properties for the text that are the same on every row
//
NSString *cellValue =[listaComentarios objectAtIndex:indexPath.row];
bottomLabel.text = cellValue;
bottomLabel.backgroundColor = [UIColor clearColor];
bottomLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
//bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
bottomLabel.font = [UIFont systemFontOfSize:13];
//
// Create a background image view.
//
cell.backgroundView =
[[[UIImageView alloc] init] autorelease];
cell.selectedBackgroundView =
[[[UIImageView alloc] init] autorelease];
}
else
{
topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
//bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
}
NSString *qlNome = [nomesComentarios objectAtIndex:indexPath.row];
topLabel.text = qlNome;
NSString *cellValue =[listaComentarios objectAtIndex:indexPath.row];
bottomLabel.text = cellValue;
//
// Set the background and selected background images for the text.
// Since we will round the corners at the top and bottom of sections, we
// need to conditionally choose the images based on the row index and the
// number of rows in the section.
//
UIImage *rowBackground;
//UIImage *selectionBackground;
rowBackground = [UIImage imageNamed:@"ComentariosBaloon.png"];
//selectionBackground = [UIImage imageNamed:@"BkgComentarios.png"];
((UIImageView *)cell.backgroundView).image = rowBackground;
//((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
return cell;
}
- (void)dealloc
{
[tabelaComentarios release];
[TBXML release];
[super dealloc];
}
@end
Thanks for any help!
In the case where a cell is successfully dequeued from the tableview, you neglect to set the pointer bottomLabel to any particular value and subsequently implicitly dereference it. If you genuinely don't want it to point to anything when a cell is dequeued then initialise it as pointing to nil:
UITextView *bottomLabel = nil;
It's always safe to message nil in Objective-C.
You've commented out the line:
//bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
Without knowing the wider context of your program, I guess it is also possible that this commenting out is an error. If you do want to set the value of bottomLabel when a cell is dequeued, you probably want to reinstate that.
精彩评论