Iphone 无法将方法放置在tableView:cellForRowAtIndexPath:

Iphone 无法将方法放置在tableView:cellForRowAtIndexPath:,iphone,objective-c,cocoa-touch,uitableview,Iphone,Objective C,Cocoa Touch,Uitableview,我的类中有一个名为alphabets的NSMutableString。我将[alphabets characterAtIndex:I]或[alphabets length]放入tableView:CellForRowAtIndex路径:。但是当我使用重载数据时,应用程序崩溃了 编辑:我应该使用tableView之外的字母表进行所有计算,然后将一个值数组传递给tableView吗 这就是“字母表”出现的地方 在@interface中 NSMutableString *alphabets; @实施

我的类中有一个名为alphabets的NSMutableString。我将[alphabets characterAtIndex:I]或[alphabets length]放入tableView:CellForRowAtIndex路径:。但是当我使用重载数据时,应用程序崩溃了

编辑:我应该使用tableView之外的字母表进行所有计算,然后将一个值数组传递给tableView吗

这就是“字母表”出现的地方

在@interface中

NSMutableString *alphabets;
@实施

- (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;
}

你的应用程序崩溃的原因可能是因为你正在调用CellForRowatineXpath中的重载数据。重新加载数据将导致CellForRowatineXpath再次启动,并且您将陷入无限循环,直到内存耗尽。尝试在CellForRowatineXpath中设置一个断点,并观察它的发生

只是一个猜测,没有看到任何代码,但是在调用CellForRowatineXpath时,“字母表”已经被垃圾回收了吗。。。?还是你已经有了保留权

给我们看一些代码片段。一个创建/实例化“字母表”的人和一个使用它的人

编辑:

根据您在问题中添加的代码片段,我认为您可能需要:

[alphabets retain];
在您上一次分配给“alphabets”之后——我不能肯定地说没有看到“[myLogic MakeAssensing:alphabets]”的实现——如果它调用了一个返回临时字符串的方法,那么您需要将其保留在成员变量(ivar)中


当然,由于您需要保留它以在您自己的对象的整个生命周期中访问它,因此您还必须在dealloc的实现中适当地释放它。

我会这样做

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

因为从示例代码来看,在有人编辑textField之前,您可能正在从未初始化的指针分配textLabel.text。

定义“应用程序崩溃”。请发布解释崩溃的控制台内容。您需要发布一些代码示例。iPhoneReally上没有垃圾收集吗?你关于iPhone上垃圾收集的信息来源是什么…?嗯,很有趣。我想我应该说“已经发布/删除”而不是“垃圾收集”——在我看来,它们在概念上非常接近。谢谢你纠正我。哦,是的,谢谢你完全正确。我到处找。我想我需要了解iphone框架或其他东西。但是没有,我犯了一个初学者的错误。考虑到我是初学者,我想这还不算太糟。再次感谢。