I am trying to create a chat room and show "available chats" on a table view. I am trying to turn the *object NSArray to a NSMutableArray using mutableCopy and display that on the tableView ... but it seems to constantly crash....
any help appreciated.
.h
@interface MMKMatchesViewController1 : UITableViewController
.m
@interface MMKMatchesViewController1 () //
@property (strong, nonatomic) IBOutlet UITableView *tableView; @property (strong, nonatomic) NSMutableArray *availableChatRooms;
The Code to update the chatrooms
-(NSMutableArray *)availableChatRooms { if (!_availableChatRooms){ _availableChatRooms = [[NSMutableArray alloc] init]; } return _availableChatRooms; }
This is what should update the tableView
(void)updateAvailableChatRooms { PFQuery *query = [PFQuery queryWithClassName:@"ChatRoom"];
[query whereKey:@"user1" equalTo:[PFUser currentUser]];
PFQuery *queryInverse = [PFQuery queryWithClassName:@"ChatRoom"];
[query whereKey:@"user2" equalTo:[PFUser currentUser]];
PFQuery *queryCombined = [PFQuery orQueryWithSubqueries:@[query, queryInverse]];
//[queryCombined includeKey:@"chat"];
[queryCombined includeKey:@"user1"];
[queryCombined includeKey:@"user2"];
[queryCombined findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error){ [self.availableChatRooms removeAllObjects]; [self.tableView reloadData]; //[self.availableChatRooms addObjectsFromArray:objects]; <---NEED HELP HERE :-( [self.availableChatRooms = objects mutableCopy]; <---NEED HELP HERE :-( [self.tableView reloadData]; }
}]; }
Table Data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.availableChatRooms count];
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFObject *chatRoom =[self.availableChatRooms objectAtIndex:indexPath.row];
PFUser *likedUser;
PFUser *currentUser = [PFUser currentUser];
PFUser *testUser1 = chatRoom[@"user1"];
if ([testUser1.objectId isEqual:currentUser.objectId]) {
likedUser =[chatRoom objectForKey:@"user2"];
} else {
likedUser = [chatRoom objectForKey:@"user1"];
}
cell.textLabel.text = likedUser[@"profile"][@"firstName"];
//344
//cell.imageView.image = place holder image
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
PFQuery *queryForPhoto = [[PFQuery alloc] initWithClassName:@"Photo"];
[queryForPhoto whereKey:@"user" equalTo:likedUser];
[queryForPhoto findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if ([objects count] > 0){ PFObject *photo = objects[0]; PFFile *pictureFile = photo[kMMKPhotoPictureKey]; [pictureFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { cell.imageView.image =[UIImage imageWithData:data]; cell.imageView.contentMode = UIViewContentModeScaleAspectFit; }]; }
}];
return cell; }
@JAMIE - The error that shows once i press the button to goto the chat window
2014-11-08 12:34:21.468 Project1[19993:244958] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:6116
2014-11-08 12:34:21.472 Project1[19993:244958] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(
0 CoreFoundation 0x0000000109727f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001093c0bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000109727d9a +[NSException raise:format:arguments:] + 106
3 Foundation 0x00000001076595df -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 UIKit 0x000000010806f864 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 153
5 Jobazo 0x000000010641877d -[MMKMatchesViewController1 tableView:cellForRowAtIndexPath:] + 109
6 UIKit 0x000000010807c4b3 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
7 UIKit 0x000000010805bfb1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
8 UIKit 0x0000000108071e3c -[UITableView layoutSubviews] + 213
9 UIKit 0x0000000107ffe973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
10 QuartzCore 0x0000000107c92de8 -[CALayer layoutSublayers] + 150
11 QuartzCore 0x0000000107c87a0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
12 QuartzCore 0x0000000107c8787e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
13 QuartzCore 0x0000000107bf563e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
14 QuartzCore 0x0000000107bf674a _ZN2CA11Transaction6commitEv + 390
15 QuartzCore 0x0000000107bf6db5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
16 CoreFoundation 0x000000010965cdc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
17 CoreFoundation 0x000000010965cd20 __CFRunLoopDoObservers + 368
18 CoreFoundation 0x0000000109652b53 __CFRunLoopRun + 1123
19 CoreFoundation 0x0000000109652486 CFRunLoopRunSpecific + 470
20 GraphicsServices 0x000000010a8ba9f0 GSEventRunModal + 161
21 UIKit 0x0000000107f85420 UIApplicationMain + 1282
22 Project1 0x0000000106417c93 main + 115
23 libdyld.dylib 0x0000000109c54145 start + 1
24 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Thanks!
Assuming this is not just a typo when you copied it to SO, your syntax is wrong. Here is your problem.
[self.availableChatRooms = objects mutableCopy]; <---NEED HELP HERE :-(
Although the fact that you said it crashes kind of confuses me, if this was the problem I'd suspect it would fail to compile.
What you want to do is
self.availableChatRooms = [objects mutableCopy];
Brackets are used for message passing in objective-c (that's and a few other things) so you'd be calling a method on availableChatRooms but you're trying to do an assignment within there.
If this isn't your problem and it is just a typo if you could provide the error that's outputted when it crashes that would help a lot.
EDIT:
According to the documentation at the developer site.
You must register a class or nib file using the
registerNib:forCellReuseIdentifier:
orregisterClass:forCellReuseIdentifier:
method before calling this method.
I don't know what the rest of your code looks like but I'd assume that's where the problem would lie.
If you're creating your table view cells within a Storyboard it'll take care of registering the nib for you and you can use the dequeueReusableCellWithIdentifier:
method, see the docs here
(Be careful as this method will just return nil if there's no cells in the queue so you'll need to create a new cell yourself if it returns nil) also make sure your cell identifiers match those in your storyboard.