首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何每x秒刷新一次随机的UITableViewCell of UITableView x。Objective-C

如何每x秒刷新一次随机的UITableViewCell of UITableView x。Objective-C
EN

Stack Overflow用户
提问于 2013-12-02 13:09:08
回答 2查看 1.2K关注 0票数 0

我在玩一个桌面视图的游戏。

这就是应用程序的想法:

我按下一个按钮,启动一个计时器,然后用随机图像填充表视图的单元格。我用了7个细胞和5个图像。根据我接触到的细胞,我得到了不同的分数。在5秒内,我要显示5个不同的单元格(1秒,1个单元格)。每次我显示一个细胞,我隐藏其他的细胞,我有可能接触这个细胞,并获得积分。在这5秒之后,我想要更改表视图的内容。

我知道如何只隐藏一个单元格,并用隐藏属性隐藏其他单元格。

问题是,我只管理显示单元格,每1秒更改一次表视图的数据,而不是在5秒之后。

我做的步骤

1)启动计时器。每5秒我就叫refreshTable

代码语言:javascript
复制
self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(refreshTable) userInfo:nil repeats:YES];

2)在refreshTable中,我用7个新对象填充了一个数组。每个对象都是一个UIImage。我从一个53 UIImages的数组中得到7 UIImages。我刷新表,用新的图像填充行。

代码语言:javascript
复制
-(void)refreshTable
{


    self.valueImage=arc4random() % 52;

    self.value=arc4random() % [self.wackArray count];


    for (int n=0 ;n<7 ; n++){

        int random=arc4random() % 52;


        [self.randomArrayIndex addObject:[NSString stringWithFormat:@"%i", random]];
        int objectRandomArray = [[self.randomArrayIndex objectAtIndex:n] integerValue];

        [self.randomArrayImage insertObject:[self.imagesArray objectAtIndex:objectRandomArray] atIndex:n];

    }

    [self.tableView reloadData];

    [self.randomArrayIndex removeAllObjects];

}

为了管理时间,我需要嵌套NSTimer还是使用performSelector?为了显示细胞,我必须使用retriveCellForIndexPathreloadRowsAtIndexPaths

调用setNeedsDisplay刷新/显示单元格是可行的吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-05 10:41:42

这是我最后的refreshTable方法。我意识到我没有使用UICollectionViewCells。

代码语言:javascript
复制
-(void)refreshTable

{

代码语言:javascript
复制
    self.tableView.hidden=NO;

   //count returns number of cells
   self.value=arc4random() % [self.wackArray count];

   [self.tableView beginUpdates];

   NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
   NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
   NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:2 inSection:0];
   NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:0];
   NSIndexPath *indexPath4 = [NSIndexPath indexPathForRow:4 inSection:0];
   NSIndexPath *indexPath5 = [NSIndexPath indexPathForRow:5 inSection:0];
   NSIndexPath *indexPath6 = [NSIndexPath indexPathForRow:6 inSection:0];

   UITableViewCell *cell0=[self.tableView cellForRowAtIndexPath:indexPath0];
   UITableViewCell *cell1=[self.tableView cellForRowAtIndexPath:indexPath1];
   UITableViewCell *cell2=[self.tableView cellForRowAtIndexPath:indexPath2];
   UITableViewCell *cell3=[self.tableView cellForRowAtIndexPath:indexPath3];
   UITableViewCell *cell4=[self.tableView cellForRowAtIndexPath:indexPath4];
   UITableViewCell *cell5=[self.tableView cellForRowAtIndexPath:indexPath5];
   UITableViewCell *cell6=[self.tableView cellForRowAtIndexPath:indexPath6];

   cell0.hidden=YES;
   cell1.hidden=YES;
   cell2.hidden=YES;
   cell3.hidden=YES;
   cell4.hidden=YES;
   cell5.hidden=YES;
   cell6.hidden=YES;

   cell0.userInteractionEnabled=NO;
   cell1.userInteractionEnabled=NO;
   cell2.userInteractionEnabled=NO;
   cell3.userInteractionEnabled=NO;
   cell4.userInteractionEnabled=NO;
   cell5.userInteractionEnabled=NO;
   cell6.userInteractionEnabled=NO;


   NSIndexPath *indexPathRandom = [NSIndexPath indexPathForRow:self.value inSection:0];
   UITableViewCell *cellRandom=[self.tableView cellForRowAtIndexPath:indexPathRandom];

   cellRandom.hidden=NO;
   cellRandom.userInteractionEnabled=YES;

   [self.tableView endUpdates];

}

票数 1
EN

Stack Overflow用户

发布于 2013-12-02 13:56:54

如果我理解了你的问题,你想一次只显示一个单元格(即1分钟),然后再把它藏起来。

您可以从UICollectionViewCell创建一个派生类,并在collectionViewDataSource中使用它们。然后,在你的计时器上,你可以使用一个NSIndexPath来获取想要的细胞,并告诉它在一段时间内动画;

例如:

CollectionViewDataSource.m

代码语言:javascript
复制
-(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"MyIdentifier";
MyCell *cell = (MyCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
[cell configureForItem:YourObjectToConfigure];
    return cell;
}



-(void)refreshTable
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:ROW inSection:SECTION];
    [collectionView cellForItemAtIndexPath:indexPath] animate];
}

MyCell.h

代码语言:javascript
复制
@interface MyCell : UICollectionViewCell
-(void) configureForItem:(NSObject*) item;
-(void) animate;

@end

MyCell.m

代码语言:javascript
复制
 @implementation MyCell
 -(void) configureForItem:(NSObject*) item{
        // Do your configuration stuff
 }

 -(void) animate{
        [UIView animateWithDuration:1 animations:^{
                  // Do your show stuff
            } completion:^{
                  // Do your hide stuff
            }];
  }
 @end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20329145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档