ちょっとここ、私はUITableViewでいくつかの問題を抱えています。UITableViewクラッシュ
XMLを使用して動的に行を作成するように私のUITableViewを設定しています。 iPhone Simulatorではすべてうまく動作しますが、デバイス上にビルドすると、テーブルを上または下にドラッグすると、アプリケーションがクラッシュします。
何かが私が気づいたことは、画面から出るときにアプリがちょうどクラッシュすることです。だから、テーブルがまだ画面に表示されているとき、アプリは正常に動作しますが、画面からドラッグするとクラッシュします。
は、ここでは、コードを行く:任意の助け
#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;
static 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
感謝を!
k、これはあまりにも多くのコードがフォーマットされていません。あなたは少し問題を絞り込む必要があります。それがクラッシュしたとき、コンソールは何を言っていますか?何が変わってクラッシュが始まったのですか? –
コードを選択し、エディタで '01010'ボタンを押すと、正しくフォーマットされます。明らかにあなたの問題に関係のないコードを入れないでください。 -numberOfSectionsInTableView:メソッド。あなたのお手伝いをしたい人のための生活を楽にしてください。そして、あなたはより速く答えを得るでしょう。 – Vladimir
あなたのコードを見ることから2つのことがあります:1)客観的なCのメモリ管理をブラッシュアップする必要があります。便利なメソッド(stringWithFormat:initWithFormatの代わりに:)を使用できるときにalloc-initを使用しているので、このコードには2つのメモリリークがあります。2)UITableViewCellにはデフォルトで2つのUILabelが既に付属しています。 –