2017-12-11 9 views
-2

私は周りを探索してきましたが、私が直面しているこの特定の問題に対する答えを見つけることができないようです。これは私が実験している一般的なチャットアプリです。私の目的は、単一のconvoまたはグループconvoの場合にのみ、特定のセクションを表示することです。あなたは特定のセクションを表示

#define bGroupNameSection 0 
#define bParticipantsSection 1 
#define bAddParticipantSection 2 
#define bLeaveConvoSection 3 
#define bSectionCount 4 

以下のコード上で見ることができるように、何が現在やっていることは私的な会話やグループチャットの両方にbGroupNameSectionある最初のセクションが表示されています。しかし、私はそれが私的な会話であるかどうかを示すために参加者セクションだけを望みます。

ここでは、何を意味するのかを理解するのに役立つスクリーンショットを示します。

Group chat

Single Chat

How it should be on a single chat

- (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

特定の問題点を明確にすることはできますか?これらのスクリーンショットが望ましい機能であるか間違っている機能であるかは明らかではなく、実際に何が実際に行われているかを識別するためのコンテキストもありません。 – Stonz2

+0

こんにちは@ stonz2最初のスクリーンショットにはグループチャットの詳細が表示され、そのセクションはどのように表示され、2番目はプライベートな会話の詳細がどのように見えるか、3番目はプライベートな会話の様子です。私は、参加者セクションが "グループ名"セクションではなく、表示されることを望みます。 – Venus007

答えて

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; 
その後

:あなたは正しく初期化インスタンス変数にセクション番号を比較することができ、あなたの関連cellForRowとheaderForRow方法で

-(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; 
} 

。例えばあなたのコードで

if (indexPath.section == self.addParticipantSection) { 
    // Handle section data 
} 
+0

驚くばかり!これは、マイクありがとう、働いた! – Venus007

0

、あなたは定数として値を事前に定義し、チャットの種類によって異なります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]; 

さて、セクションの数には、セクションでは、INS sectionArrayは行数で

を数える返す

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 
{ 
    //... 
} 
関連する問題