2011-12-09 46 views
0

因此,我正在编写一个应用程序来读取rss提要,并在tableview中显示内容。它还可以让用户播放它为每个项目找到的MP3。无论如何,应用程序似乎运行良好,然后我开始添加新视图。现在,每当我从视图中回来并滚动一下时,就会看到“节目接收信号”SIGABRT“”或类似的东西。返回到tableview后崩溃

这里的大部分程序:

- (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 
+0

不要在你的'viewDidLoad:'中释放iVar,你应该用'delloc'方法代替它。删除这个:'[self.feed release];'。我无法确定它会解决您的问题。 :) – Kjuly

+0

因此,当它在'mainLabel.text = entry.articleTitle'上崩溃时,它实际上指向了一个错误的地址。我只是不知道我的RSSEntry对象是如何改变的。 – Build

+0

@Kjuly噢好点。 – Build

回答

-1

没有线你在哪里得到它的崩溃很难说,但最有可能你访问某个对象发生了什么dealloc'ed

最有可能的位置

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

您立即释放对象,但很难说不知道您是否保留了属性

+0

有时它会在行上崩溃“mainLabel.text = entry.articleTitle;” – Build

+0

虽然我很难看到RSSEntry会被释放,但当视图切换时我不会释放任何东西。我不这么认为...... – Build

+0

不是一个好的模式,但如果feed是一个'保留'属性,那么这里最有可能的就是ok。 (我们不知道,但应该期待) – Eiko

1

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

+0

良好的通话,但我修好了,它仍然是相同的情况。任何时候我离开桌面并返回它崩溃。 – Build

+0

我相信你会发布一些你不应该使用的变量。尝试对所有可变参数使用NSAutoreleasePool。 – StackFlowed

1

这是一个疯狂的猜测,但你不保留您在viewDidLoad获得的图像:

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

无论是使用固定财产和点语法或发送的每一个retain

+0

啊,我没有在那里使用保留,所以是的,我修复了这个问题。尽管如此,仍然崩溃。 – Build

+0

如果它在mainLabel.text = ...行上崩溃,请在该行之前设置一个断点并检查标签和条目。 – Eiko

0

请不要松开self.feed以及当卸载或dealloc的,当时把代表零的观点意味着

tableview.delegate =零;

这一个是主要的事情之后,我认为你不要无表的代表。

+0

你的意思是在-dealloc方法中我应该释放委托?不知道。但是直到程序的最后才会调用dealloc。 – Build

1

AHAA !!!在将它放入_allEntries数组之前,我将RSSEntry设置为autorelease。当我改变视图时,他们正在解除分配。不要这样做。感谢大家的帮助。那太简单了,我现在觉得很愚蠢。

+0

所以,恭喜! ;) – Kjuly