返回tableview后崩溃

时间:2011-12-09 08:23:06

标签: iphone objective-c ios uitableview memory-management

所以我正在编写一个应用程序来读取RSS源,并在tableview中显示内容。它还允许用户播放它为每个项目找到的mp3。无论如何,在我开始添加新视图之前,应用程序似乎运行正常。现在,每当我从一个视图回来并滚动一点时,我得到“程序接收信号”SIGABRT“”或类似的东西。

这是该计划的大部分内容:

- (IBAction)playAction:(id)sender
{
// Get row
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = 
(UITableViewCell *) [[senderButton superview] superview];
NSInteger buttonRow = [[self.tableView 
                        indexPathForCell:buttonCell] row];

// Entry for row
RSSEntry *senderEntry = [_allEntries objectAtIndex:buttonRow];


// This is where _allEntries gets filled

- (void)requestFinished:(ASIHTTPRequest *)request {

[_queue addOperationWithBlock:^{

    NSError *error;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData]
                                                           options:0 error:&error];

    if (doc == nil) 
    {
        NSLog(@"Failed to parse %@", request.url);
    } 
    else 
    {

        NSMutableArray *entries = [NSMutableArray array];
        [self parseRss:doc.rootElement entries:entries]; 

        if ([_allEntries count] > 0) {

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                // Update
                int i=0;
                while (![[[_allEntries objectAtIndex:i] articleUrl] isEqualToString:[[entries objectAtIndex:i] articleUrl]]) 
                {
                    [_allEntries insertObject:[entries objectAtIndex:i] atIndex:0];
                    i++;
                }
                [self.tableView reloadData];
            }];

        }
        else
        {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                for (RSSEntry *entry in entries)
                {
                    [_allEntries addObject:entry];
                }

                NSLog(@"entries:%d", [_allEntries count]);
                [self.tableView reloadData];
            }];
        }


    }

}];



}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"View did load");

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
                                          initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh 
                                          target:self 
                                          action:@selector(refreshButton:)];

pauseImage = [UIImage imageNamed:@"pause_circle_small.png"];
playImage = [UIImage imageNamed:@"play_circle_small.png"];

player = nil;
isPlaying = NO;

self.title = @"Feed";
self.allEntries = [NSMutableArray array];
self.queue = [[[NSOperationQueue alloc] init] autorelease];
self.feed = [[NSString alloc] initWithString:@"http://site.org/rss/"];
[self refresh];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_allEntries count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UILabel *mainLabel, *secondLabel;
UIButton *playBtn;

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

    mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(42.0, 5.0, 250.0, 20.0)] autorelease];
    mainLabel.tag = MAINLABEL_TAG;
    mainLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:18.0];
    mainLabel.textAlignment = UITextAlignmentLeft;
    mainLabel.textColor = [UIColor blackColor];
    mainLabel.highlightedTextColor = [UIColor whiteColor];
    [cell.contentView addSubview:mainLabel];

    secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(42.0, 27.0, 250.0, 15.0)] autorelease];
    secondLabel.tag = SECONDLABEL_TAG;
    secondLabel.font = [UIFont fontWithName:@"ArialMT" size:14.0];
    secondLabel.textAlignment = UITextAlignmentLeft;
    secondLabel.textColor = [UIColor colorWithRed:222.0/255.0 green:95.0/255.0 
                                             blue:199.0/255.0 alpha:1.0];
    secondLabel.highlightedTextColor = [UIColor whiteColor];
    [cell.contentView addSubview:secondLabel];

    playBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    playBtn.tag = PLAYBTN_TAG;
    playBtn.frame = CGRectMake(2.0, 6.0, playImage.size.width, playImage.size.height);
    [playBtn setBackgroundImage:playImage forState:UIControlStateNormal];
    //[playBtn setBackgroundImage:playImage forState:UIControlStateHighlighted];

    [playBtn addTarget:self action:@selector(playTapped:) 
      forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:playBtn];
}
else 
{
    mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
    secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
    playBtn = (UIButton *)[cell.contentView viewWithTag:PLAYBTN_TAG];
}

// Alternate bg color
if (indexPath.row%2 == 0) {
    UIColor *altColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0
                                         blue:230.0/255.0 alpha:1];
    mainLabel.backgroundColor = altColor;
    secondLabel.backgroundColor = altColor;
}
else
{
    UIColor *altColor = [UIColor colorWithRed:255.0 green:255.0
                                         blue:255.0 alpha:1];
    mainLabel.backgroundColor = altColor;
    secondLabel.backgroundColor = altColor;
}

RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
NSLog(@"Entry: %@", entry);

// Manage play button
if (entry == currEntry)
{
    if(isPlaying)
    {
        [playBtn setBackgroundImage:pauseImage forState:UIControlStateNormal];
    }
    else
    {
        [playBtn  setBackgroundImage:playImage forState:UIControlStateNormal];
    }
}
else
    [playBtn setBackgroundImage:playImage forState:UIControlStateNormal];

mainLabel.text = entry.articleTitle;
secondLabel.text = entry.articleArtist;

return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.

DetailView *detailViewController = [[DetailView alloc] initWithNibName:@"DetailedView" bundle:[NSBundle mainBundle]];

RSSEntry *entry = [_allEntries objectAtIndex:[indexPath row]];



[self.navigationController pushViewController:detailViewController animated:YES];

detailViewController.songTitle.text = entry.articleTitle;
detailViewController.artistName.text = entry.articleArtist;

[entry release];

[detailViewController release];

}

- (void)dealloc
{
[player release];
player = nil;
[_queue release];
_queue = nil;
[_feed release];
_feed = nil;
[_allEntries release];
_allEntries = nil;

[super dealloc];
}

@end

5 个答案:

答案 0 :(得分:1)

请不要发布任何@synthesize变量。你应该只用dealloc方法

发布它

答案 1 :(得分:1)

这是一个疯狂的猜测,但你没有保留viewDidLoad中的图像:

pauseImage = [UIImage imageNamed:@"pause_circle_small.png"];
playImage = [UIImage imageNamed:@"play_circle_small.png"];

使用保留属性和点语法,或者发送每个retain

答案 2 :(得分:1)

AHAA !!!在将它们放入_allEntries数组之前,我将自己的RSSEntry设置为autorelease。当我改变观点时,他们得到了dealloc。不要那样做。感谢大家的帮助。这很简单,我现在感到愚蠢。

答案 3 :(得分:0)

请不要释放self.feed,当卸载或取消分配当时的视图时,委托nil意味着

tableview.delegate = nil;

这个是主要的事情检查后我认为你不要没有tableview的代表。

答案 4 :(得分:-1)

没有你遇到崩溃的行很难说,但很可能你访问了dealloc'ed的一些对象

很可能就是这里

self.feed = [[NSString alloc] initWithString:@"http://site.org/rss/music"];
[self.feed release];

你立刻释放物品,但不知道你是否保留了财产

很难说清楚