滚动时自定义表格视图单元格标签隐藏如何解决?

时间:2013-10-15 06:51:39

标签: iphone ios objective-c ipad cocoa-touch

  

“我正在使用自定义单元格类来显示它成功显示的时间,但问题是当我向下滚动表格视图,之后那个计时器标签没有显示。我已经使用了这段代码但是当我向下滚动并再次向上计时器隐藏的单元格标签,我没有得到如何解决这个问题“

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
      {
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
    cell=customCell;

}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
    MyCell *cell = (MyCell *)[tableView cellForRowAtIndexPath:indexPath];

  [cell startTimer:indexPath];
   } 

//这里是mycell类代码

  - (void) startTimer:(NSIndexPath *)indexPath
   {


if (self.timer)

    [self.timer invalidate];


  currentIndexPath=indexPath;
   self.secondsLeft=10;


  self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self    selector:@selector(calculateTimer) userInfo:nil repeats:YES];

  [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

   [self.timer fire];
 }

   - (void)calculateTimer
  {

if (self.secondsLeft >0)
{

    self.secondsLeft --;
    self.timeLbl.text=[NSString stringWithFormat:@"0%d",self.secondsLeft];
    self.showLbl.text=[NSString stringWithFormat:@"0%d",self.secondsLeft];

}
if (self.secondsLeft==0)
{
    [self.timer invalidate];
    self.timeLbl.text=@"";
}

  }
   -(void)showTimer
    {
   self.timeLbl.text=[NSString stringWithFormat:@"%d",self.secondsLeft];
  NSLog(@"this is self second%d",self.secondsLeft);


}

2 个答案:

答案 0 :(得分:0)

这意味着它不会刷新在您的方法中包含这些行: -

[tableView reloadData];

答案 1 :(得分:0)

试试这个......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
      {
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

if (cell == nil) {
    cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
    cell=customCell;

}

将ReuseIdentifier设置为nil。

如果您有任何问题,请与我们联系。

更新的答案

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:nil];

    if (cell == nil) {

        cell = (CustomCell *)[[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] objectAtIndex:0];
    }
cell.textLabel.text=@"XYZ";
}