2016-04-28 6 views
0

私はこのようにUITableViewCellを作成しています。その画面では、1つのクイズ、2つのクイズ、1つの投票、2つの投票などがあります。これは動的になります。iOS uitableviewcellに動的サブビューを追加する

cellで受信したデータに基づいて、ユーザーが上下にスクロールすると、以前のUIViewsが削除され続け、何度も何度も再作成されます。 (私はそれが超悪いです知っている。今私のスクロールが問題だ。)私はパフォーマンスを向上させるためにしなければならない方法

enter image description here

NSArray *quizzez = self.cellData[SERVER_QUIZZES]; 
    NSArray *polls = self.cellData[SERVER_POLLS]; 
    NSMutableArray *combinedQuizPoll = [NSMutableArray array]; 
    [combinedQuizPoll addObjectsFromArray:quizzez]; 
    [combinedQuizPoll addObjectsFromArray:polls]; 

    for (UIView *vw in self.quizPollViewCollection) { 
     [vw removeFromSuperview]; 
    } 

    for (NSDictionary *quizPollDict in combinedQuizPoll) 
    {    
     QuizPollSubView *vwQuizPoll = [QuizPollSubView loadFromNibWithType:QuizPollSubViewNoViewRelated andNavType:self.navType]; 
     [vwQuizPoll setW:CGRectGetWidth(self.frame)]; 
     [vwQuizPoll setDelegate:self]; 
     [vwQuizPoll setData:muQuizPollDict]; 
     [vwQuizPoll setX:0 andY:offset]; 
     [self.contentView addSubview:vwQuizPoll]; 
     offset = CGRectGetMaxY(vwQuizPoll.frame) + 4; 
     [self.quizPollViewCollection addObject:vwQuizPoll]; 
    } 

?私はStackOverflowでも同様の質問をしました。

How to make a UITableViewCell with different subviews reusable?

1)私は、世論調査)は、セル毎に異なるものとなる動的なクイズ、投票ビュー(クイズの数を持っている必要があります

2)私は、私が作成したものを図を参照することができますどのように?私はあなたのアプローチが垂直で物事を置くために同じセルを使用することを言っているすべての

+0

カスタムデキューを追加するのUITableViewをサブクラス化する必要があります。http://stackoverflow.com/questions/ 5746904/how-to-make-a -uitableview-cell-with-different-subviews-reusable?lq = 1 –

答えて

0

ファーストは最高のものではありません。この種の状況では、複数のセルを使用する必要があります。以下のような何か:

  • ...
  • DecriptionCell
  • QuizCell
  • QuizCell
  • PollCell
  • PollCell
  • PollCell
  • ...

とにかく、あなたのUITableViewの構造を変更することなくあなたを助けることができるソリューションを提案します。

実際に私は数週間前に同じ問題を抱えていましたが、そのための非常に良い解決策が見つかりました。

基本的にメインコンセプトは、パフォーマンスが影響を受けることになりますので、あなたは、セルの中でのconfigureのビューを追加または削除するべきではないのUITableViewCellを再利用すること、です。

私が使っていた解決策は、セルが持つことができる構成の種類ごとに異なる再使用識別子を使用することでした。

独自の要件は、セルのNibファイルを持つことではありません。

私が正しく理解している場合、あなたの細胞は、ダイナミクスQuizsと投票を持つことができます。最大10回のクイズと最大10回の投票ということにしましょう。私は両方が同じビューを持っていることを見ているが、QuizPollSubView。ですから、1つのセルにつき最大20のサブビューを作成しましょう。

だから私は、次の操作を行います細胞を登録する方法で:

Class myClass = [CustomTableViewCell class]; 
NSString *classID = NSStringFromClass(myClass); 
for (NSUInteger index = 0; index < 20; index++) { 
    NSString *identifier = [classID stringByAppendingString:[@(index) stringValue]]; 
    [self.tableView registerClass:myClass forCellReuseIdentifier:identifier]; 
} 

次にCellForRowであなたは、例えば、properIdentifierでセルをデキューする必要があります。

NSString *cellID = NSStringFromClass([CustomTableViewCell class]); 
NSUInteger numberOfQuizsAndPolls = 3 + 2; //This is 3 quizs and 2 polls, I gess that you can read from the DataModel 
NSString *identifier = [cellID stringByAppendingString:[@(numberOfQuizsAndPolls) stringValue]]; 
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 

//then configure the cell 

を次に、initWithStyle:reuseIdentifierで、空の値を持つサブビューを作成し、識別子から情報を抽出します。

NSString *stringNumber = [reuseIdentifier stringByReplacingOccurrencesOfString:NSStringFromClass([self class]) 
                     withString:@""]; 
NSUInteger numberOfSubviews = [stringNumber integerValue]; 

//here you should add all of your QuizPollSubView with emtpy content. 
for (NSUInteger index = 0; index < numberOfSubviews; index++) { 
    QuizPollSubView *vwQuizPoll = [QuizPollSubView loadFromNibWithType:QuizPollSubViewNoViewRelated andNavType:self.navType]; 
    [vwQuizPoll setW:CGRectGetWidth(self.frame)]; 
    [vwQuizPoll setDelegate:self]; 
    //[vwQuizPoll setData:muQuizPollDict]; YOU CAN NOT SET THE DATA HERE BECAUSE YOU DONT HAVE IT 
    [vwQuizPoll setX:0 andY:offset]; 
    [self.contentView addSubview:vwQuizPoll]; 
    offset = CGRectGetMaxY(vwQuizPoll.frame) + 4; 
    [self.quizPollViewCollection addObject:vwQuizPoll]; 
} 

最後に、セルの構成に適切な情報を設定する必要があります。何かのように:

- (void)configureWithQuizPollDict:(NSDictionary *)combinedQuizPoll 
{ 
    for (NSDictionary *quizPollDict in combinedQuizPoll) 
    { 
     //get the proper index in the quizPollViewCollection. 
     QuizPollSubView *vwQuizPoll = self.quizPollViewCollection[index]; 
     [vwQuizPoll setData:muQuizPollDict]; 
    } 
} 

私はそれがあなたを助けることを願っています!

おかげ

PD:あなたはペン先を有するセルを使用したい場合は、おそらく私たちはあなたを助けることができるかもしれない

関連する問題