解决方案 »

  1.   

    不管UI是怎么样的,只是表现形式不同,内部还是通过一个NSTimer来不停地回调吧
      

  2.   

    在多线程下使用NSTimer 来创建倒计时实例。 [NSThread detachNewThreadSelector:@selector(handleTimer:) toTarget:self withObject:nil];
    - (void)handleTimer{
        // 子线程,需要手动加入一个释放池,来释放通过便利构造器创建的对象,主线程自动添加了
        @autoreleasepool {
            [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(ontime) userInfo:nil repeats:YES];
            // 开启Runloop来使线程保持存活状态
            [[NSRunLoop currentRunLoop] run];
        }
    }
      

  3.   

    还是一样的~最开始我用的GCD也这样。
      

  4.   

    你说说ontime只调用了一次?调用了 都有调用 但是 也只有一个cell里面的计时器在走 其他就调用一次 刷新下就固定在那个时间了
      

  5.   

    你说说ontime只调用了一次?调用了 都有调用 但是 也只有一个cell里面的计时器在走 其他就调用一次 刷新下就固定在那个时间了
    我问下,你是一个cell对应一个timer,还是全局只有一个timer?
      

  6.   

    你说说ontime只调用了一次?调用了 都有调用 但是 也只有一个cell里面的计时器在走 其他就调用一次 刷新下就固定在那个时间了
    我问下,你是一个cell对应一个timer,还是全局只有一个timer?全局只有一个timer  每个cell都会调用它
      

  7.   

    你说说ontime只调用了一次?调用了 都有调用 但是 也只有一个cell里面的计时器在走 其他就调用一次 刷新下就固定在那个时间了
    我问下,你是一个cell对应一个timer,还是全局只有一个timer?全局只有一个timer  每个cell都会调用它
    我去,只一个timer实例,它能不出现你说的问题吗!  你需要在每个cell中都创建一个timer实例。每个timer实体作用在每个cell上。
      

  8.   

    你说说ontime只调用了一次?调用了 都有调用 但是 也只有一个cell里面的计时器在走 其他就调用一次 刷新下就固定在那个时间了
    我问下,你是一个cell对应一个timer,还是全局只有一个timer?全局只有一个timer  每个cell都会调用它
    我去,只一个timer实例,它能不出现你说的问题吗!  你需要在每个cell中都创建一个timer实例。每个timer实体作用在每个cell上。每调用一次不是重新创建了个timer实例了么?
      

  9.   

    你说说ontime只调用了一次?调用了 都有调用 但是 也只有一个cell里面的计时器在走 其他就调用一次 刷新下就固定在那个时间了
    我问下,你是一个cell对应一个timer,还是全局只有一个timer?全局只有一个timer  每个cell都会调用它
    我去,只一个timer实例,它能不出现你说的问题吗!  你需要在每个cell中都创建一个timer实例。每个timer实体作用在每个cell上。每调用一次不是重新创建了个timer实例了么?
    是在每个cell内部都有一个NSTimer实例。 写一个自定义UITableViewCell的类,封装timer倒计时的实现 。
      

  10.   

    你说说ontime只调用了一次?调用了 都有调用 但是 也只有一个cell里面的计时器在走 其他就调用一次 刷新下就固定在那个时间了
    我问下,你是一个cell对应一个timer,还是全局只有一个timer?全局只有一个timer  每个cell都会调用它
    我去,只一个timer实例,它能不出现你说的问题吗!  你需要在每个cell中都创建一个timer实例。每个timer实体作用在每个cell上。每调用一次不是重新创建了个timer实例了么?
    是在每个cell内部都有一个NSTimer实例。 写一个自定义UITableViewCell的类,封装timer倒计时的实现 。UITableViewCell是自定义的  nstimer如何封装呢 我现在处于用了各种方法都显示不了的 节奏~
      

  11.   

    你说说ontime只调用了一次?调用了 都有调用 但是 也只有一个cell里面的计时器在走 其他就调用一次 刷新下就固定在那个时间了
    我问下,你是一个cell对应一个timer,还是全局只有一个timer?全局只有一个timer  每个cell都会调用它
    我去,只一个timer实例,它能不出现你说的问题吗!  你需要在每个cell中都创建一个timer实例。每个timer实体作用在每个cell上。每调用一次不是重新创建了个timer实例了么?
    是在每个cell内部都有一个NSTimer实例。 写一个自定义UITableViewCell的类,封装timer倒计时的实现 。UITableViewCell是自定义的  nstimer如何封装呢 我现在处于用了各种方法都显示不了的 节奏~
    把你cell的代码贴出来看看吧。
      

  12.   

    你说说ontime只调用了一次?调用了 都有调用 但是 也只有一个cell里面的计时器在走 其他就调用一次 刷新下就固定在那个时间了
    我问下,你是一个cell对应一个timer,还是全局只有一个timer?全局只有一个timer  每个cell都会调用它
    我去,只一个timer实例,它能不出现你说的问题吗!  你需要在每个cell中都创建一个timer实例。每个timer实体作用在每个cell上。每调用一次不是重新创建了个timer实例了么?
    是在每个cell内部都有一个NSTimer实例。 写一个自定义UITableViewCell的类,封装timer倒计时的实现 。UITableViewCell是自定义的  nstimer如何封装呢 我现在处于用了各种方法都显示不了的 节奏~
    把你cell的代码贴出来看看吧。cell 不就是个布局嘛
      

  13.   

    if (tableView == hotSale)
        {
            static NSString *string = @"Cell";
            
            cell1=[tableView dequeueReusableCellWithIdentifier:string];
            if (!cell1)
            {
                cell1 = [[HotSaleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
                cell1.accessoryType = UITableViewCellAccessoryNone;
                cell1.backgroundColor=[UIColor colorWithRed:244/255.0 green:238/255.0 blue:233/255.0 alpha:1];
            }
            //        图片
            [cell1.hs_headImage setImageWithURL:[NSURL URLWithString: [[_hSArr objectAtIndex:indexPath.row]_hs_gImg]]];
            
            //      show zanwutu.jpg  Image data
            if (cell1.hs_headImage.image == nil) {
                [cell1.hs_headImage setImage:[UIImage imageNamed:@"zanwutu.jpg"]];
            }
            
            
            //        品牌英文
            cell1.hs_eName.text=[[_hSArr objectAtIndex:indexPath.row] _hs_ge_name];
            //        品牌中文
            cell1.hs_cName.text=[[_hSArr objectAtIndex:indexPath.row] _hs_gc_name];
            
            //        特卖价格
            float floatString1 = [[[_hSArr objectAtIndex:indexPath.row] _hs_shopPrice] floatValue];//转化
            [cell1.shopPrice setText:[NSString stringWithFormat:@"%.2f",floatString1]];
            //        原价
            float floatString2 = [[[_hSArr objectAtIndex:indexPath.row] _hs_Price] floatValue];
            [cell1.Price setText:[NSString stringWithFormat:@"%.2f",floatString2]];
            //        绘制删除线
            cell1.Price.attributedText = [[NSAttributedString alloc]initWithString:cell1.Price.text attributes:@{NSStrikethroughStyleAttributeName:[NSNumber numberWithInt:2]}];
            //        库存
            cell1.stock.text=[[_hSArr objectAtIndex:indexPath.row] _hs_gNum];
            
            //        英文国家
            cell1.nationEname.text=[[_hSArr objectAtIndex:indexPath.row] _hs_eName];        //        中文国家
            cell1.nationCname.text=[[_hSArr objectAtIndex:indexPath.row] _hs_cName];
            
            //        倒计时字符串
            self.timeStr = [[_hSArr objectAtIndex:indexPath.row] _hs_AddTime];
            
    //        [self getFormat];
            
            //        开启线程
    //        [NSThread detachNewThreadSelector:@selector(startTimer) toTarget:self withObject:nil];
            
            return cell1;
        }这个是-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath里面的
      

  14.   

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // Initialization code
            //        图片
            _hs_headImage = [[UIImageView alloc]initWithFrame:CGRectMake(5,22,90,88)];
            [self.contentView addSubview:_hs_headImage];
            
            //      英文名字
            _hs_eName = [[UILabel alloc]initWithFrame:CGRectMake(105,4,200,30)];
            _hs_eName.font = [UIFont systemFontOfSize:14];
            _hs_eName.textColor = [UIColor grayColor];
            _hs_eName.numberOfLines =0;
            _hs_eName.lineBreakMode = UILineBreakModeWordWrap;
            [self.contentView addSubview:_hs_eName];
            
            //       汉文名字
            _hs_cName = [[UILabel alloc]initWithFrame:CGRectMake(105,29,200,30)];
            _hs_cName.font = [UIFont systemFontOfSize:14];
            _hs_cName.textColor = [UIColor grayColor];
            _hs_cName.numberOfLines =0;
            _hs_cName.lineBreakMode = UILineBreakModeWordWrap;
            [self.contentView addSubview:_hs_cName];
            
            //      价格 字符
            _priceText = [[UILabel alloc]initWithFrame:CGRectMake(105,54,50,30)];
            _priceText.font = [UIFont systemFontOfSize:14];
            _priceText.text = @"价格:";
            _priceText.textColor=[UIColor grayColor];
            [self.contentView addSubview:_priceText];
            
            //      价格 数字
            _shopPrice = [[UILabel alloc]initWithFrame:CGRectMake(143,54,60,30)];
            _shopPrice.font = [UIFont boldSystemFontOfSize:14];
            _shopPrice.textColor=[UIColor redColor];
            [self.contentView addSubview:_shopPrice];
            
            //      价格 灰色数字
            _Price = [[UILabel alloc]initWithFrame:CGRectMake(200,54,60,30)];
            _Price.font = [UIFont systemFontOfSize:14];
            _Price.textColor=[UIColor grayColor];
            _Price.baselineAdjustment = UIBaselineAdjustmentAlignCenters;//文本中线与label中线对齐
            [self.contentView addSubview:_Price];
            
            //      库存 字符
            _stockText = [[UILabel alloc]initWithFrame:CGRectMake(220,70,50,30)];
            _stockText.font = [UIFont systemFontOfSize:14];
            _stockText.text = @"已销售:";
            _stockText.textColor=[UIColor grayColor];
            [self.contentView addSubview:_stockText];        //      库存
            _stock = [[UILabel alloc]initWithFrame:CGRectMake(275,70,20,30)];
            _stock.font = [UIFont systemFontOfSize:14];
            _stock.textColor=[UIColor redColor];
            [self.contentView addSubview:_stock];
            //       国家
            _nationText=[[UILabel alloc]initWithFrame:CGRectMake(105,92, 35, 20)];
            _nationText.font = [UIFont systemFontOfSize:14];
            _nationText.text = @"国家:";
            _nationText.textColor=[UIColor grayColor];
            [self.contentView addSubview:_nationText];
            
            //    英文名字
            _nationEname=[[UILabel alloc]initWithFrame:CGRectMake(145, 92,80, 20)];
            _nationEname.font = [UIFont systemFontOfSize:14];
            _nationEname.textColor=[UIColor grayColor];
            [self.contentView addSubview:_nationEname];
            
            //   汉文名字
            _nationCname=[[UILabel alloc]initWithFrame:CGRectMake(230, 92, 65, 20)];
            _nationCname.font = [UIFont systemFontOfSize:14];
            _nationCname.textColor=[UIColor grayColor];
            [self.contentView addSubview:_nationCname];
            
            //   剩余时间字符
            _haveTimeText = [[UILabel alloc]initWithFrame:CGRectMake(105, 110, 63, 20)];
            _haveTimeText.font = [UIFont systemFontOfSize:14];
            _haveTimeText.text = @"剩余时间:";
            _haveTimeText.textColor = [UIColor grayColor];
            [self.contentView addSubview:_haveTimeText];
            
            //  倒计时
            _howTime = [[UILabel alloc]initWithFrame:CGRectMake(170, 110, 500, 20)];
            _howTime.font = [UIFont systemFontOfSize:14];
            _howTime.numberOfLines = 0;
            _howTime.textColor = [UIColor grayColor];
            _howTime.lineBreakMode = UILineBreakModeWordWrap;
            _howTime.baselineAdjustment = UIBaselineAdjustmentAlignCenters;//文本中线与label中线对齐
            [self.contentView addSubview:_howTime];
        }
        return self;
    }这个是自定义cell 布局  不知道理解对不对 都发上来了  NStimer 代码不能实现就不发了。
      

  15.   

    楼盖的太高了,换个楼。
    你自定义cell的代码中什么也没有处理吗?我说的封装NStimer就是让你在自定义的cell中实现 ,如
    @interface youCustomCell:UITableViewCell 
    @property (nonatomic,strong) NSTimer *timer;
    //////通过IBoutlet 关联xib的插座变量就不写了
    -(void)configCell:(id)data;
    -(void)startTimer;
    @end在实现文件中写你的倒计时逻辑
      

  16.   

    http://blog.sina.com.cn/s/blog_4cfd0d760101rhkh.html 这个博客 表示没看懂 大神可以看下
      

  17.   

    我觉得不能在cell里创建timer,因为cell是可复用的,难道你不需要为每条记录对应一个cell吗?如果每个cell的timer都是相互独立的,那么这个timer应该和数据源绑定
      

  18.   

    我建议这样:
    首先你的数据源肯定是一个NSArray,其中每一个对象肯定是你自定义的对象,在对象中加入一个数字,表示倒计时,然后在对象内创建和启动timer,回调给自己,这样倒计时这块就独立封装好了;由于数据源不应该关心自己被哪里展示,所以应该由cell去取对象中的倒计时数据,简单点的话,就在ViewController里创建和启动一个timer,这个timer调用的时候reloadData(也只是reload当前屏幕中所见的cell而已),然后cell根据对象中的数据更新即可。这其中也有很多优化区间,比如数据源很多的时候,也不应该同时启动太多timer,这个需要根据你所存的数据和业务逻辑来优化。
      

  19.   

    我建议这样:
    首先你的数据源肯定是一个NSArray,其中每一个对象肯定是你自定义的对象,在对象中加入一个数字,表示倒计时,然后在对象内创建和启动timer,回调给自己,这样倒计时这块就独立封装好了;由于数据源不应该关心自己被哪里展示,所以应该由cell去取对象中的倒计时数据,简单点的话,就在ViewController里创建和启动一个timer,这个timer调用的时候reloadData(也只是reload当前屏幕中所见的cell而已),然后cell根据对象中的数据更新即可。这其中也有很多优化区间,比如数据源很多的时候,也不应该同时启动太多timer,这个需要根据你所存的数据和业务逻辑来优化。正在测试您的建议~谢谢指导·~~
      

  20.   

    我想问cell的复用,你认为复用的是什么? 从单一职责来讲我不建议把timer封装到数据源,反而我认为是破坏了封装。
      

  21.   

    我想问cell的复用,你认为复用的是什么? 从单一职责来讲我不建议把timer封装到数据源,反而我认为是破坏了封装。cell复用的不就是cell麽 
      

  22.   

    本帖最后由 zhangao0086 于 2015-01-08 17:40:20 编辑
      

  23.   

    *** -[CFString _fastCharacterContents]: message sent to deallocated instance 0xff09bf0