我正在嘗試在UITableCell(設計爲IB)的子類中正確設置觸摸UIButton事件的目標,該子類將刪除該單元。然而,當在模擬器跑了,我得到以下錯誤:在目標方法中傳遞UIButton參數nil
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* - [__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
它得到的方法很好,但它始終是NSArray的arrayWithObject線後立即崩潰。看起來這是因爲傳遞給目標方法的按鈕始終爲零。
我假設這是一個內存問題,但我很困惑如何解決它。我是否需要完全編程創建單元以使其工作,或者是否有一種簡單的方法來以某種方式將按鈕操作的目標指定爲Interface Builder的主ViewController?
這裏就是細胞在視圖控制器創建:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==songList){
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
SongCell *cell = (SongCell *)[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SongCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
[cell.addButton addTarget:self action:@selector(addToPlaylist:) forControlEvents:UIControlEventTouchUpInside];
}
NSUInteger row = [indexPath row];
cell.songname.text = @"test";//[songListData objectAtIndex:row];
return cell;
}
return nil;
}
而這裏的目標方法:
-(void)addToPlaylist:(id)button{
SongCell *song = (SongCell *)[(UIButton *)button superview];
NSIndexPath *indexPath = [self.songList indexPathForCell:song];
NSArray *rowToMove = [NSArray arrayWithObject:indexPath];
[self.songList beginUpdates];
[self.songList deleteRowsAtIndexPaths:rowToMove withRowAnimation:UITableViewRowAnimationFade];
[self.songList endUpdates];
}
,你可以提供任何幫助將不勝感激。
嘗試記錄'button'和'indexPath'。我的第一個猜測是Interface Builder已經自動調整了視圖層次結構,以便你的按鈕的超級視圖不再是'SongCell'本身。 – Tommy