2016-04-28 104 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添加自定义出队

相关问题