I have TableView. My datasource is SQLite database. I’m trying to count a number of comments to some message (I know message id and it’s text) for numberOfRowsInSection: method.
-(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;
}
It isn’t work even NSLog. I think this method should be called in viewDidLoad() but not sure that way is right.
Editing
Comments.h
@interface Comments : NSObject {
NSString *messageID;
NSString *commentText;
}
@property (nonatomic, retain) NSString *messageID;
@property (nonatomic, retain) NSString *commentText;
-(id)initWithName:(NSString *)mID commentText:(NSString *)cText;
MessageID I get from previous view:
- (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;
}
It would also be interesting to know exactly what’s not working. Does your application crash at NSLog or does it return the wrong message count?
Since you’re returning
numberOfCommentsForMessagefrom the method it would be desirable to reduce the scope ofnumberOfCommentsForMessageto a local variable within the method.