gpt4 book ai didi

ios - 解析聊天室 - 将 NS 数组转换为 NSMutableArray 以在 TableView 上显示

转载 作者:行者123 更新时间:2023-11-28 21:55:15 26 4
gpt4 key购买 nike

我正在尝试创建一个聊天室并在表格 View 中显示“可用聊天”。我正在尝试使用 mutableCopy 将 *object NSArray 转换为 NSMutableArray 并将其显示在 tableView 上......但它似乎经常崩溃......

感谢任何帮助。

.h

@interface MMKMatchesViewController1 : UITableViewController

.m

@interface MMKMatchesViewController1 () //

@property (strong, nonatomic) IBOutlet UITableView *tableView; @property (strong, nonatomic) NSMutableArray *availableChatRooms;

更新聊天室的代码

-(NSMutableArray *)availableChatRooms { if (!_availableChatRooms){ _availableChatRooms = [[NSMutableArray alloc] init]; } return _availableChatRooms; }

这是应该更新 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];


    }

    }]; }

表数据源

  • (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 - 我按下按钮转到聊天窗口后显示的错误

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 处的文档.

You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

我不知道您的其余代码是什么样的,但我认为这就是问题所在。

如果您在 Storyboard 中创建表格 View 单元格,它将负责为您注册 Nib ,您可以使用 dequeueReusableCellWithIdentifier: 方法,请参阅文档 here

(请注意,如果队列中没有单元格,此方法将只返回 nil,因此如果它返回 nil,您需要自己创建一个新单元格)还要确保您的单元格标识符与 Storyboard 中的标识符相匹配。

关于ios - 解析聊天室 - 将 NS 数组转换为 NSMutableArray 以在 TableView 上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26819136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com