UITableView崩溃没有错误 [英] UITableView crashing with no erros

查看:291
本文介绍了UITableView崩溃没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,从导航控制器的根,一个表视图,我构建一个URL字符串,其中包含我想显示在新的tableView的信息。在新表视图中,我使用NSURL请求的URL。我的代码给我没有错误,但崩溃,紧接着NSLog:made细胞。帮助之前我杀了自己。抱歉应该都在一起,但复制粘贴代码显然不能很好地工作。

  #importCatDisplayViewController.h
#importCatViewController.h


实现CatDisplayViewController

@synthesize downvotebutton;
@synthesize upvotebutton;
// @ synthesize tripsHold;
@synthesize URLtoUse;






NSArray * trips;


- (void)viewDidLoad {
[super viewDidLoad];



NSURL * url = [NSURL URLWithString:URLtoUse];

NSLog(@%@,url);

responseData = [[NSMutableData data] retain];

NSURLRequest * request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

}

   - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@recieved response);
[responseData setLength:0];

}

   - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@Did receiveieve);
[responseData appendData:data];

}

   - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@connection failed);
NSLog(@%@,error);

}

   - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSLog(@我们的连接完成加载);
NSString * responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];

NSLog(@%@,responseString);

NSLog(@released data);
NSDictionary * results = [responseString JSONValue];
NSLog(@built JSON dictionary);
NSArray * allTrips = [results objectForKey:@results];
NSLog(@built dictionary from dictionary);

trips = allTrips;
NSLog(@done with connectFinish method);

[self.tableView reloadData];

}

  #pragma mark  -  
#pragma mark表视图数据源



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//返回节的数量。

NSLog(@in sections number);

return 1;

}

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//返回节中的行数。
NSLog(@%@,trips);
return [trips count];

}

   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
NSLog(@in height for row);
return 80;
}


//自定义表视图单元格的外观。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@Trying to build table);
static NSString * CellIdentifier = @Cell;

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

//配置单元格...
NSLog(@made cell);

NSDictionary * trip = [trips objectAtIndex:[indexPath row]];

NSLog(@from dictionary);

cell.textLabel.text = [trip objectForKey:@txt];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.minimumFontSize = 10;
cell.textLabel.numberOfLines = 4;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

cell.detailTextLabel.text = [trip objectForKey:@name];

UIButton * upvote = [UIButton buttonWithType:UIButtonTypeRoundedRect];

UIImage * upVoteBack = [UIImage imageNamed:@arrowup.png];
[upvote setBackgroundImage:upVoteBack forState:UIControlStateNormal];
[upvote setTitle:@+forState:UIControlStateNormal];
upvote.frame = CGRectMake(250.0f,40.0f,25.0f,25.0f);
[upvote addTarget:self action:@selector(upvoteaction:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:upvote];

UIButton * downvote = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage * downVoteBack = [UIImage imageNamed:@arrowdown.png];
[upvote setBackgroundImage:downVoteBack forState:UIControlStateNormal];
[downvote setTitle:@ - forState:UIControlStateNormal];
downvote.frame = CGRectMake(250.0f,40.0f,25.0f,25.0f);
[downvote addTarget:self action:@selector(downvoteaction:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:downvote];



// NSURL * url = [NSURL URLWithString:[aTweet objectForKey:@profile_image_url]];
// NSData * data = [NSData dataWithContentsOfURL:url];
//cell.imageView.image = [UIImage imageWithData:data];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;

}

解决方案

  trips = [allTrips retain]; 

您的旅行数组正在自动释放 - 请尝试保留并确认是否仍然收到错误。 p>

Basically, from the root of the navigation controller, a table view, I build a URL string that contains the information i want to display on the new tableView. in the new table view i use the URL for an NSURL request. My code gives me no error but crashes, right after the NSLog: made cells. help before i kill myself. sorry should all be together but copy pasting code obviously doesn't work well.

#import "CatDisplayViewController.h"
#import "CatViewController.h"


implementation CatDisplayViewController

@synthesize downvotebutton;
@synthesize upvotebutton;
//@synthesize tripsHold;
@synthesize URLtoUse;






NSArray *trips;


- (void)viewDidLoad {
[super viewDidLoad];



NSURL *url = [NSURL URLWithString:URLtoUse];

NSLog(@"%@", url);

responseData = [[NSMutableData data] retain];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@" recieved response ");
[responseData setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@" Did recieve data ");
[responseData appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@" connection failed ");
NSLog(@"%@", error);

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSLog(@" our connect finished loading");
NSString *responseString = [[NSString alloc] initWithData:responseData     encoding:NSUTF8StringEncoding];
[responseData release];

NSLog(@"%@", responseString);

NSLog(@" released data ");
NSDictionary *results = [responseString JSONValue];
NSLog(@" built JSON dictionary ");
NSArray *allTrips = [results objectForKey:@"results"];
NSLog(@" built array from dictionary ");

trips = allTrips;
NSLog(@" done with connectFinish method ");

[self.tableView reloadData];

}

#pragma mark -
#pragma mark Table view data source



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.

NSLog(@" in sections number ");

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSLog(@"%@", trips);
return [trips count];

}

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSLog(@" in height for row ");
return 80;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@" Trying to build table ");
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
NSLog(@"made cells");

NSDictionary *trip = [trips objectAtIndex:[indexPath row]];

NSLog(@" made dictionary from array ");

cell.textLabel.text = [trip objectForKey:@"txt"];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.minimumFontSize = 10;
cell.textLabel.numberOfLines = 4;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

cell.detailTextLabel.text = [trip objectForKey:@"name"];

UIButton *upvote =[UIButton buttonWithType:UIButtonTypeRoundedRect];

UIImage *upVoteBack = [UIImage imageNamed:@"arrowup.png"];
[upvote setBackgroundImage:upVoteBack forState:UIControlStateNormal];
[upvote setTitle:@"+" forState:UIControlStateNormal];
upvote.frame = CGRectMake(250.0f, 40.0f, 25.0f, 25.0f);
[upvote addTarget:self action:@selector(upvoteaction:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:upvote];

UIButton *downvote =[UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *downVoteBack = [UIImage imageNamed:@"arrowdown.png"];
[upvote setBackgroundImage:downVoteBack forState:UIControlStateNormal];
[downvote setTitle:@"-" forState:UIControlStateNormal];
downvote.frame = CGRectMake(250.0f, 40.0f, 25.0f, 25.0f);
[downvote addTarget:self action:@selector(downvoteaction:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:downvote];



//NSURL *url = [NSURL URLWithString:[aTweet objectForKey:@"profile_image_url"]];
//NSData *data = [NSData dataWithContentsOfURL:url];
//cell.imageView.image = [UIImage imageWithData:data];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;

}

解决方案

trips = [allTrips retain];

Your trips array is being autoreleased - try retaining it and see if you still get the error.

这篇关于UITableView崩溃没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆