我正在尝试创建一个聊天室,并在表格视图上显示“可用的聊天”。
我正在尝试使用mutableCopy将* object NSArray转换为NSMutableArray并将其显示在tableView上...但是它似乎经常崩溃。
任何帮助表示赞赏。
。H
@interface MMKMatchesViewController1:UITableViewController
.m
@interface MMKMatchesViewController1()//
@属性(强,非原子)IBOutlet UITableView * tableView;
@属性(强,非原子)NSMutableArray * availableChatRooms;
更新聊天室的代码
-((NSMutableArray *)availableChatRooms {
如果(!_availableChatRooms){
_availableChatRooms = [[NSMutableArray alloc] init];
}
返回_availableChatRooms; }
这是应该更新tableView的内容
(void)updateAvailableChatRooms {
PFQuery * query = [PFQuery queryWithClassName:@“ ChatRoom”];
[查询whereKey:@“ user1” equalTo:[PFUser currentUser]];
PFQuery * queryInverse = [PFQuery queryWithClassName:@“ ChatRoom”];
[查询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];
}
}]; }
表格数据源
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)节{
返回[self.availableChatRooms计数];
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
静态NSString * CellIdentifier = @“ Cell”;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
PFObject *聊天室= [self.availableChatRooms objectAtIndex:indexPath.row];
PFUser * likedUser;
PFUser * currentUser = [PFUser currentUser];
PFUser * testUser1 = chatRoom [@“ user1”];
如果([testUser1.objectId isEqual:currentUser.objectId]){
likedUser =[chatRoom objectForKey:@"user2"];
}
其他{
likedUser = [chatRoom objectForKey:@"user1"];
}
cell.textLabel.text = LikedUser [@“ profile”] [@“ firstName”];
// 344
//cell.imageView.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;
}];
}
}];
返回单元}
@JAMIE-当我按下按钮进入聊天窗口时显示的错误
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
谢谢!
最佳答案
假设将其复制到SO时这不仅是一个错字,那么您的语法是错误的。这是你的问题。
[self.availableChatRooms = objects mutableCopy]; <---NEED HELP HERE :-(
尽管您所说的崩溃的事实使我感到困惑,但是如果这是我怀疑的问题,它将无法编译。
你想做的是
self.availableChatRooms = [objects mutableCopy];
括号用于在Objective-C中传递消息(还有其他一些事情),因此您将在availableChatRooms上调用方法,但您尝试在其中进行赋值。
如果这不是您的问题,并且只是拼写错误,您可以提供崩溃时输出的错误,这将大有帮助。
编辑:
根据the developer site上的文档。
在调用此方法之前,必须使用
registerNib:forCellReuseIdentifier:
或registerClass:forCellReuseIdentifier:
方法注册类或nib文件。我不知道您其余的代码是什么样子,但我认为这就是问题所在。
如果您要在情节提要中创建表视图单元,则将为您注册笔尖,并且可以使用
dequeueReusableCellWithIdentifier:
方法,请参阅文档here(请注意,如果队列中没有单元格,此方法将仅返回nil,因此如果返回nil,则需要自己创建一个新的单元格),还要确保您的单元格标识符与情节提要中的单元格标识符匹配。