我一直在寻找,但似乎无法找到我遇到的这个特殊问题的答案。这是我正在尝试的一般聊天应用程序,我的目的是仅显示某个部分,如果它是单个convo或组convo。正如您在下面的代码中看到的那样
#define bGroupNameSection 0
#define bParticipantsSection 1
#define bAddParticipantSection 2
#define bLeaveConvoSection 3
#define bSectionCount 4
所以它当前正在做的是在私人对话或群聊中显示第一部分bGroupNameSection。但是我只希望参与者部分显示它是否是私人谈话。
以下是一些帮助理解我的意思的截图。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//to check if its a group or single chat and to return the amount of sections.
return _thread.type.intValue == bThreadTypePrivateGroup ? bSectionCount : 1;
}
我可以在这里进行任何形式的条件检查
if(it is a private chat) {
// make bParticipantsSection 0
bAddParticipantSection 1
bLeaveConvoSection 2
bSectionCount 3
remove bGroupNameSection
}
以下是可能有用的任何其他相关代码。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == bParticipantsSection) {
return _users.count ? _users.count : 1;
}
if (section == bLeaveConvoSection || section == bAddParticipantSection || section == bGroupNameSection) {
return 1;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:bCell];
cell.textLabel.textColor = [UIColor blackColor];
if (indexPath.section == bParticipantsSection) {
if (_users.count) {
CGSize itemSize = CGSizeMake(0, 0);
id<PUser> user = _users[indexPath.row];
cell.textLabel.text = user.name;
cell.imageView.image = user && user.thumbnail ? [UIImage imageWithData:user.thumbnail] : [NSBundle chatUIImageNamed: @"icn_user.png"];
cell.imageView.layer.cornerRadius = 20;
cell.imageView.clipsToBounds = YES;
itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
else {
cell.textLabel.text = [NSBundle t:bNoActiveParticipants];
cell.imageView.image = nil;
}
cell.textLabel.textAlignment = _users.count ? NSTextAlignmentLeft : NSTextAlignmentCenter;
cell.selectionStyle = _users.count ? UITableViewCellSelectionStyleDefault :UITableViewCellSelectionStyleNone;
return cell;
}
if (indexPath.section == bAddParticipantSection) {
// Reset the image view
cell.imageView.image = nil;
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = [NSBundle t:bAddParticipant];
}
if (indexPath.section == bLeaveConvoSection) {
// Reset the image view
cell.imageView.image = nil;
cell.textLabel.text = [NSBundle t:bLeaveConversation];
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
if (indexPath.section == bGroupNameSection) {
UITextField *groupNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(10,10, 300, 30)];
groupNameTextField.delegate = self;
groupNameTextField.returnKeyType = UIReturnKeyDone;
groupNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
groupNameTextField.text = _thread.displayName;
[cell.contentView addSubview:groupNameTextField];
}
return cell;
}
答案 0 :(得分:0)
问题是你已经将组部分定义为0,所以即使你在私人聊天中,你的代码也会认为它的组(给它错误的标题和单元格)。
我建议将 #defines 中的节号移动到您初始化的实例变量中。例如,在您的私人界面中:
@property (nonatomic, assign) NSInteger groupNameSection;
@property (nonatomic, assign) NSInteger participantSection;
@property (nonatomic, assign) NSInteger addParticipantSection;
@property (nonatomic, assign) NSInteger leaveConvoSection;
@property (nonatomic, assign) NSInteger sectionCount;
@property (nonatomic, assign) BOOL isPrivateChat;
然后:
-(void)viewDidLoad
{
[super viewDidLoad];
NSInteger sectionCount = 0;
self.groupNameSection = self.isPrivateChat ? sectionCount++ : -1; // If private chat, groupName section -1, otherwise its 0 (and sectionCount is incremented for next)
self.participantSection = sectionCount++;
self.addParticipantSection = sectionCount++;
self.leaveConvoSection = sectionCount++;
self.sectionCount = sectionCount;
}
在相关的cellForRow和headerForRow方法中,您可以将节号与正确初始化的实例变量进行比较。 e.g。
if (indexPath.section == self.addParticipantSection) {
// Handle section data
}
答案 1 :(得分:0)
在您的代码中,您将值预定义为常量,并将其与indexpath.section进行比较,indexpath.section将根据聊天类型而有所不同。
您应该创建一个数组,以根据聊天类型(单个或组)显示要显示的部分数组。声明部分数组:
@property (nonatomic, assign) NSArray * sectionArray;
初始化剖面数组
//For Single Chat
self.sectionArray = [NSArray arrayWithObjects:
[NSNumber numberWithInt:bParticipantsSection], nil];
//For Group
self.sectionArray = [NSArray arrayWithObjects:
[NSNumber numberWithInt:bGroupNameSection],
[NSNumber numberWithInt:bParticipantsSection],
[NSNumber numberWithInt:bAddParticipantSection],
nil];
现在,在多个部分中,返回sectionArray count
在行数中,
if ([self.sectionArray objectAtIndex: section] == [NSNumber numberWithInt:bGroupNameSection])
{
return 1;
}
else if ([self.sectionArray objectAtIndex: section] == [NSNumber numberWithInt:bAddParticipantSection])
{
return 1;
}
else
{
//...
}
在cellForRow中,
if ([self.sectionArray objectAtIndex: indexpath.section] == [NSNumber numberWithInt:bGroupNameSection])
{
//return group name cell
}
else
{
//...
}