2016-04-28 108 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

也許這可以幫助你:http://stackoverflow.com/questions/ 5746904 /如何對做-A-的UITableViewCell與 - 不同-子視圖重複使用的?LQ = 1 –

回答

0

首先,我不得不說,使用同一個單元格來放置垂直方向的方法並不是最好的方法。對於這種情況,您應該使用多個單元格。喜歡的東西:

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

無論如何,我會建議你一個解決方案,可以幫助你而不用改變你的UITableView的結構。

其實我幾周前也遇到了同樣的問題,我發現了一個很好的解決方案。

基本上主要的概念是,重用UITableViewCell不應該在單元的配置中添加或刪除視圖,因爲性能會受到影響。因此,我使用的解決方案是,爲細胞可以擁有的各種配置使用不同的重用標識符。

獨特的要求是不要爲單元格有一個Nib文件。

如果我理解正確,你的單元格可以有動態測驗和民意調查。我們來說說最多10個測驗和最多10個投票。雖然我在觀察它們都具有相同的視圖,QuizPollSubView。所以我們每個單元最多放置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:如果你想使用筆尖一個細胞可能是我們需要繼承的UITableView添加自定義出隊

相關問題