Objective c 在viewDidLoad()中调用方法。使用indexath

Objective c 在viewDidLoad()中调用方法。使用indexath,objective-c,ios,uitableview,sdk,Objective C,Ios,Uitableview,Sdk,我有桌面视图。我的数据源是SQLite数据库。我正试图为numberofrowsinssection:方法计算一些消息(我知道消息id和它的文本)的注释数 -(NSInteger)numberOfCommentsForMessage:(NSIndexPath *)indexPath { MDAppDelegate *appDelegate = (MDAppDelegate *)[[UIApplication sharedApplication] delegate]; Commen

我有桌面视图。我的数据源是SQLite数据库。我正试图为
numberofrowsinssection:
方法计算一些消息(我知道消息id和它的文本)的注释数

-(NSInteger)numberOfCommentsForMessage:(NSIndexPath *)indexPath {
    MDAppDelegate *appDelegate = (MDAppDelegate *)[[UIApplication sharedApplication] delegate];
    Comments *comment = (Comments *)[appDelegate.comments objectAtIndex:indexPath.row];
    if (MessageID == [comment.messageID integerValue]) {
        numberOfCommentsForMessage++;
     }
    NSLog(@"number of comments = %i",numberOfCommentsForMessage);
    return numberOfCommentsForMessage;
}
甚至NSLog都不起作用。我认为应该在
viewDidLoad()
中调用此方法,但不确定这种方法是否正确

编辑

评论

@interface Comments : NSObject {
    NSString *messageID;
    NSString *commentText;
}

@property (nonatomic, retain) NSString *messageID;
@property (nonatomic, retain) NSString *commentText;

-(id)initWithName:(NSString *)mID commentText:(NSString *)cText;
我从上一个视图中获得的消息ID:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
          MessageText:(NSString *)messageText
          MessageDate:(NSString *)messageDate
            MessageID:(NSInteger)messageID;
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        MessageText = [NSString stringWithString:messageText];
        MessageDate = [NSString stringWithString:messageDate];
        MessageID = messageID;
    }
    return self;
}

确切地知道什么不起作用也很有趣。您的应用程序是在NSLog崩溃还是返回错误的消息计数

由于您正在从该方法返回
numberOfCommentsForMessage
,因此需要将
numberOfCommentsForMessage
的范围缩小为该方法中的局部变量

-(NSInteger)numberOfCommentsForMessage:(NSIndexPath *)indexPath {
    NSInteger numberOfComments = 0;
    MDAppDelegate *appDelegate = (MDAppDelegate *)[[UIApplication sharedApplication] delegate];
    Comments *comment = (Comments *)[appDelegate.comments objectAtIndex:indexPath.row];
    if (MessageID == [comment.messageID integerValue]) {
        numberOfComments++;
     }
    NSLog(@"number of comments = %d",numberOfComments);
    return numberOfComments;
}

我们需要更多的代码。请在定义了MessageID的地方发布您的Comments.h头文件。但是您现在在哪里调用
numberOfCommentsForMessage:
?它被调用了吗?如果你不止一次地调用它,你不断地增加计数器,如果你有10条注释,你的注释数第一次将是10条,然后是20条,然后是30条等等。这就是为什么你应该在方法中使用局部变量而不是实例变量,请参阅我的帖子。@VadimYelagin我将其称为另一个TableView方法,如numberOfRowsInSection:、textForRowAtIndexPath:等。应用程序未崩溃。我认为这个方法根本不需要调用。但是为什么
numberOfCommentsForMessage
不能是局部变量呢?即使我合成了它?因为您都在返回值并递增实例变量,这看起来像是一个bug和/或反模式。如果它没有崩溃,你会发现什么是错误的?任何错误,任何其他错误。可能根本没有调用此方法。这就是为什么我要问如何在viewDidLoad中调用它。我在理解你的英语时有些困难,但是要在objective C中用一个参数调用一个类实例方法,语法应该是:
[self-someMethod:apaparameter]
,将其添加到
viewDidLoad
方法中。您可能想查阅一本基本的Objective C书籍,了解有关该语言的一些一般知识。