我正在尝试创建一个聊天室,并在表格视图中显示“可用聊天” . 我试图使用mutableCopy将*对象NSArray转换为NSMutableArray并在tableView上显示...但它似乎不断崩溃....
任何帮助赞赏 .
. H
@interface MMKMatchesViewController1:UITableViewController
.M
@interface MMKMatchesViewController1()// @property(强,非原子)IBOutlet UITableView * tableView; @property(强,非原子)NSMutableArray * availableChatRooms;
更新聊天室的代码
- (NSMutableArray *)availableChatRooms {if(!availableChatRooms){ availabilityChatRooms = [[NSMutableArray alloc] init]; } return _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]; <---需要帮助:-(
[self.availableChatRooms = objects mutableCopy]; <---需要帮助:-(
[self.tableView reloadData];
}
}]; }
表数据源
(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 * likesUser; PFUser * currentUser = [PFUser currentUser]; PFUser * testUser1 = chatRoom [@“user1”]; if([testUser1.objectId isEqual:currentUser.objectId]){lovedUser = [chatRoom objectForKey:@“user2”];
} else {likesUser = [chatRoom objectForKey:@“user1”];
} cell.textLabel.text = likesUser [@“profile”] [@“firstName”]; // 344 //cell.imageView.image =占位符图像cell.imageView.contentMode = UIViewContentModeScaleAspectFit; PFQuery * queryForPhoto = [[PFQuery alloc] initWithClassName:@“Photo”]; [queryForPhoto whereKey:@“user”equalTo:likesUser]; [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
谢谢!
2 回答
假设当你将它复制到SO时这不仅仅是一个拼写错误,你的语法是错误的 . 这是你的问题 .
虽然你说它崩溃的事实让我困惑,如果这是问题,我怀疑它将无法编译 .
你想做的是
括号用于在objective-c中传递消息(这是其他一些东西)所以你要在availableChatRooms上调用一个方法,但是你试图在那里做一个赋值 .
如果这不是你的问题而且只是一个错字,如果你可以提供崩溃时输出的错误,这将有很大帮助 .
编辑:
根据the developer site的文件 .
我不知道你的代码的其余部分是什么样的,但我认为这就是问题所在 .
如果您负责为您注册笔尖并且可以使用
dequeueReusableCellWithIdentifier:
方法,请参阅文档here(请注意,如果队列中没有单元格,此方法将返回nil,因此如果返回nil,您将需要自己创建一个新单元格)同时确保您的单元格标识符与故事板中的单元格标识符匹配 .
这是一个非常优秀的bug .
[]需要左侧的对象和右侧的消息 .
对象是
您在此处使用赋值运算符,它将“对象”分配给“self.availableChatRooms”,该赋值运算符的结果是self.availableChatRooms,它与对象相同 .
然后将“mutableCopy”消息发送给对象 . 这会创建一个可变副本,并且因为您没有告诉编译器如何处理结果,所以被扔掉了 . 你写的是和
你自找的
我建议你在Xcode中打开所有警告 . 我没有尝试过,但希望你的代码会产生一些警告 .