2016-10-12 4 views
1

私は前に仕事をしたことがあるコードを使っています。本質的に、ユーザーは、いくつかのボタンを使って投稿に「はい」または「いいえ」と答えます。 yesまたはnoを押すと、正常に動作するデータベースが更新され、動作していない表示可能なUIも更新されます。このUIはボタンを更新してボタンが選択され、その他が強調表示され、ユーザーの操作のために両方が無効になります。また、2つのUILabelを変更します。これらのボタンが呼び出すメソッドは、データベースを更新し、tableViewCellからボタンを取得し、変更を更新する必要があります。別のViewControllerで動作するメソッドがあるので、ここでその違いを理解できません。ここに私のcellForRowAtIndexPathテーブルビューviewWithTagはUIElementsを取得していませんobjective-c

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *simpleTableIdentifier = [NSString stringWithFormat:@"%ld,%ld",(long)indexPath.section,(long)indexPath.row]; 
NSLog(@" simple: %@",simpleTableIdentifier); 
if (indexPath.row==0) { 
    ProfileFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[ProfileFirstCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:simpleTableIdentifier]; 
    } 


    cell = [self createProfileCell:cell]; 
    return cell; 

}else{ 
    YesNoCell *cell =[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell==nil) { 
     cell=[[YesNoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 
    cell = [self createYesNoCell:cell:indexPath]; 
    return cell; 

    } 
} 

は、基本的にこれが何をするか最初のセル内のユーザプロファイルを作成し、ユーザーが求めていることを、すべての質問にロードされています。これは、古いtableViewとこのtableViewの間に見られる大きな違いです。 createYesNoCellで、私はのUIElementを作成し、

cell.yesVoteButton.tag=indexPath.row+ yesVoteButtonTag1; 
    cell.noVoteButton.tag=indexPath.row+ noVoteButtonTag1; 
    cell.yesCountLabel.tag=indexPath.row+ yesCountLabelTag1; 
    cell.noCountLabel.tag=indexPath.row+ noCountLabelTag1; 

ボタンを次のようにタグを作成するものの数を開始セレクタを持っています。次のボタンでどのボタンが押されたかを検出します。

 NSInteger index; 
if(sender.tag>=yesVoteButtonTag1){ 
    NSLog(@"Yes button pressed"); 
    votedYes=true; 
    index=sender.tag-yesVoteButtonTag1; 

}else{ 
    NSLog(@"No button Pressed"); 
    votedYes=false; 
    index=sender.tag-noVoteButtonTag1; 
} 

    UILabel *yesLabel = (UILabel*) [self.tableView viewWithTag:index+yesCountLabelTag1]; // you get your label reference here 
    UIButton *yesButton=(UIButton *)[self.tableView viewWithTag:index+1+yesVoteButtonTag1]; 
    NSLog(@"Tag IN METHOD: %ld",index+yesVoteButtonTag1); 
    UILabel *noLabel = (UILabel*) [self.tableView viewWithTag:index+1+noCountLabelTag1]; // you get your label reference here 
    UIButton *noButton=(UIButton *)[self.tableView viewWithTag:index+noVoteButtonTag1]; 

これらのviewWithTagコールは、それらを見るとnilです。私の以前の実装から見ることができる唯一の違いは、古いものはセクションと1つの行を持ち、これはすべて行と1つのセクションです。だからindexPath.rowでindexPath.sectionを置き換えることはそれを考慮する必要があります。また、cellForRowAtIndexPathで作成されたタグが、yes/no voteメソッドで復元された行と同じであることを確認しました。これは、indexPath.row == 0で作成されたプロファイルセルのために1つ置き換えられたためです。私はyes/no投票メソッドにセルを渡そうとしましたが、同様の投稿に対していくつかの提案としてcontentViewを使ってボタンとラベルを復元しようとしました。しかし、これは私の問題を解決するように見えませんでした。本当にこれについてのいくつかの洞察に感謝します。

答えて

0

あなたは '[tableView reload]'メソッドを呼び出してUITableViewを更新すると便利です。

0

最初に、表の再利用識別子はタイプのセルであり、各セルには1つではありません。 2つのタイプがあるので、2つの固定再利用識別子を使用する必要があります。それらが作成されたとき

ProfileFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ProfileCell"]; 

if (cell == nil) { 
    cell = [[ProfileFirstCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:@"ProfileCell"]; 
} 

YesNoCell *cell =[self.tableView dequeueReusableCellWithIdentifier:@"YesNoCell"]; 
if (cell==nil) { 
    cell=[[YesNoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YesNoCell"]; 
} 

は第二に、あなたのために働いていないテーブルを作成した後にセルへの参照を取得しようとするのではなく、あなたは完全に細胞を初期化する必要があります。 (TableViewでは表示されない限りセルは作成されないので、既存のものに依存しないでください)

createProfileCell実際にはinitializeProfileCellと呼ばれるはずです上記の行でそれをしたか、古いものを回復しました。

initializeProfileCellを呼び出すと、それがYesセルかNoセルかを指定し、それに応じてプロパティを設定するフラグを設定できます。

cell = [self initializeProfileCell:cell isYes:(indexPath.section==0)]; 

createYesNoCell->initializeYesNoCellと同様に、

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"YOURCELL_IDENTIFIER"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    UILabel *title = (UILabel*) [cell viewWithTag:5]; 
    UILabel *vensu =(UILabel*) [cell viewWithTag:7]; 
    vensu.text = @"YOUR TEXT"; 

    title.text = @"YOUR TEXT"; 

    return cell; 
} 
関連する問題