-2

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

 - (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);


}
4

2 回答 2

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";
}
于 2013-10-15T07:24:24.477 回答
0

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

[tableView reloadData];
于 2013-10-15T06:56:40.133 回答