2011-04-05 62 views
0

我在我的類中有一個名爲字母的NSMutableString。我在tableView:cellForRowAtIndexPath:中放置了[字母characterAtIndex:i]或[字母長度]。但是,當我使用reloaddata應用程序崩潰。不能在tableView中放置方法:cellForRowAtIndexPath:

編輯:我應該使用tableView以外的字母進行所有計算,然後將值數組傳遞給tableView。

這就是 「字母」 出現

在@interface

NSMutableString *alphabets; 

@implementation

- (IBAction) textFieldDoneEditing: (id)sender { 

    Logic *myLogic = [[Logic alloc] init]; 
    alphabets = [NSMutableString stringWithCapacity:0]; 

    alphabets = [myLogic formatSentence: sentenceTextField.text]; 
    alphabets = [myLogic makeAscending: alphabets]; 

    [logicTable reloadData]; 

    // removes keyboard 
    [sentenceTextField resignFirstResponder]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

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

    // this causes all the problem 
    cell.textLabel.text = alphabets; 

    return cell; 
} 
+2

定義'應用程序崩潰'。請發佈解釋崩潰的控制檯內容。 – pt2ph8 2011-04-05 17:28:49

+1

你將需要發佈一些代碼示例。 – Joe 2011-04-05 17:29:05

回答

1

只是猜測沒有看到任何代碼,但是「字母」已經被垃圾收集的時間cellForRowAtIndexPath被稱爲...?或者你有保留嗎?

向我們展示一些代碼片段。一個創建/實例化「字母表」和一個使用它的...

編輯:

根據你在你的問題已經添加的代碼片段,它看起來像你對我可能需要:

[alphabets retain]; 

在您最後一次賦予「字母」後 - 我不能確定沒有看到「[myLogic makeAscending:alphabets]」的實現 - 如果它調用一個返回臨時字符串的方法,你需要保留它以保存在一個成員變量(ivar)中。

當然,由於您需要保留它以在您自己的對象的整個生命週期中訪問它,所以您還必須在實現dealloc時適當地釋放它。

+2

iPhone上沒有垃圾回收 – vikingosegundo 2011-04-05 22:02:35

+0

真的嗎?什麼是關於iPhone上垃圾收集的信息來源......? – DLRdave 2011-04-06 19:12:18

+1

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html – 2011-04-06 19:16:44

0

您的應用程序崩潰的原因很可能是因爲您呼叫reloadData的cellForRowAtIndexPath裏面。 reloadData將導致cellForRowAtIndexPath再次觸發,並且直到內存不足時纔會陷入無限循環。嘗試在你的cellForRowAtIndexPath中設置斷點,並觀察它發生

+0

不,我沒有reloaddata cellForRowAtIndexPath – shashu10 2011-04-05 21:17:31

0

我會做這樣

if (alphabets) { 
    cell.textLabel.text = alphabets; 
} 

東西,因爲從你的示例代碼,似乎你可以從一個未初始化的指針,直到別人在編輯文本框來分配textLabel.text。

相關問題