2011-12-12 29 views
0

我有一個自定義表格單元格類。難以置信,從類中檢索一個字符串

TextInput.h

@interface TextInput : UITableViewCell <UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> { 
    UILabel *cellLabel; 
    UITextField *textFieldBox; 
    NSString *imageFile; 
} 

@property (nonatomic, retain) UITextField *textFieldBox; 
@property (nonatomic, retain) UILabel *cellLabel; 
@property (nonatomic, retain) NSString *imageFile; 

它裏面有一個攝像頭按鈕,圖像保存到本地文件夾。我將文件名保存到imageFile中,該文件使用唯一的名稱生成。

Textinput.m

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    //retrieve image 
    UIImage *image = [info objectForKey: @"UIImagePickerControllerOriginalImage"]; 

    imageFile = [self findUniqueSavePath]; 

    [self saveImage:image:imageFile]; 
    //dismiss the camera 
    CoreTableAppDelegate *mainDelegate = (CoreTableAppDelegate *) [[UIApplication sharedApplication] delegate]; 
    [mainDelegate.rootViewController dismissModalViewControllerAnimated: YES]; 
} 

- (NSString *)findUniqueSavePath { 
    NSString *path; 
    CFUUIDRef uuid = CFUUIDCreate(NULL); 
    NSString *uuidString = [(NSString *)CFUUIDCreateString(NULL, uuid) autorelease]; 
    CFRelease(uuid); 
    path = [uuidString stringByAppendingPathExtension: @"png"]; 
    return path; 
} 

一切正常,除非我想要從父的UITableView類的文件的名稱罰款。

EditTankDescription.m

- (void)getCell 
{ 

    TextInput *cell1 = (TextInput *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];  
    NSLog(@"save imageFile:%@", cell1.imageFile); <***** crashes on this line 

} 

它崩潰與一個EXC_BAD_ACCESS的NSLog的聲明。這就像imageFile不是NNString。如果我設置imageFile = @「SOME隨機字符串」,沒有錯誤。 我很難過。

感謝您的任何見解。

回答

1
imageFile = [self findUniqueSavePath]; 

你不持有imageFile伊娃。當你去閱讀字符串時,它是一個懸掛指針,你得到EXC_BAD_ACCESS。

相反,使用屬性訪問器,它將處理釋放和保留你:

self.imageFile = [self findUniqueSavePath]; 

或者你可以直接設置伊娃,在這種情況下,你需要自己做內存管理:

[imageFile release]; 
imageFile = [[self findUniqueSavePath] retain]; 
0

在getCell的第一行放置一個斷點。 cell1很可能是零。 您的索引路徑是否正確?也許第零部分只有一排?行也將被零索引:

[NSIndexPath indexPathForRow:0 inSection:0] 
+0

不錯的想法,但cell1確實返回一個TextInput的uitableview單元格。如果我檢索一個隨機字符串,那很好。它只與imageFile崩潰。 – Mistergreen

+0

我想我得出的結論是UITableViewCell類有一些奇怪的地方。它不喜歡非視圖類型的屬性。所以,如果我將我的文件名存儲到一個UIable中並像NSString myFile = cell1.cellLable.text那樣檢索它,那麼它工作正常。 – Mistergreen

相關問題