Ios 特定UITableViewCell中的目标特定UILabel
我正在构建一个应用程序,在该应用程序中,我单击“开始”按钮,它将在所选单元格的标签中运行NSTimer。我用的是一个原型电池,里面有两个标签:名字和时间。这些标签都有自己的标签:分别为1010和1020。在运行时,有10个生成的单元格。NSArray正在填充数据。基本上,我要找的是在选定的单元格中启动计时器。如果没有在每个单元格中运行计时器,我似乎无法做到这一点。在我的项目中,我只使用一(1)个带有一(1)个标识符的原型单元 总之,我一直遇到的问题是,计时器在每个单元格中运行。我想知道如何瞄准一个特定的细胞。我已经储存了手机号码。我能用它做什么 感谢您花时间阅读本文,如果您希望我提供更多信息来帮助您回答问题,我将非常乐意这样做Ios 特定UITableViewCell中的目标特定UILabel,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在构建一个应用程序,在该应用程序中,我单击“开始”按钮,它将在所选单元格的标签中运行NSTimer。我用的是一个原型电池,里面有两个标签:名字和时间。这些标签都有自己的标签:分别为1010和1020。在运行时,有10个生成的单元格。NSArray正在填充数据。基本上,我要找的是在选定的单元格中启动计时器。如果没有在每个单元格中运行计时器,我似乎无法做到这一点。在我的项目中,我只使用一(1)个带有一(1)个标识符的原型单元 总之,我一直遇到的问题是,计时器在每个单元格中运行。我想知道如何瞄准一
int cellNumber;
BOOL isCurrentlyRunning = NO;
- (void)viewDidLoad{
[super viewDidLoad];
//Table Data Code
self.peopleArray = @[@"Person 1", @"Person 2", @"Person 3", @"Person 4", @"Person 5", @"Person 6", @"Person 7", @"Person 8", @"Person 9", @"Person 10"];
self.TimeArray = @[@"0:00", @"0:00", @"0:00", @"0:00", @"0:00", @"0:00", @"0:00", @"0:00", @"0:00", @"0:00", @"0:00"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.peopleArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"SimpleIdentifier"];
UILabel *lblName = (UILabel *)[cell viewWithTag:1010];
UILabel *lblTime = (UILabel *)[cell viewWithTag:1020];
NSString *Name, *Time;
Name = [self.roundOrder objectAtIndex:indexPath.row];
lblTime.text = [self formattedTime:self.currentTimeInSeconds];
lblName.text = Name;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[_myTimer invalidate];
self.currentTimeInSeconds = 0;
cellNumber = [indexPath row];
}
- (int)timeToStop{
int retVal = 0;
switch (cellNumber) {
case 0:
retVal = 100;
break;
default:
retVal = 200;
break;
}
return retVal;
}
- (NSTimer *)createTimer {
return [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerTicked:)
userInfo:nil
repeats:YES];
}
- (NSString *)formattedTime:(int)totalSeconds
{
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
return [NSString stringWithFormat:@"%d:%02d", minutes, seconds];
}
- (void)timerTicked:(NSTimer *)timer {
_currentTimeInSeconds++;
if (self.currentTimeInSeconds == [self timeToStop]){
[_myTimer invalidate];
}
self.lblTimer.text = [self formattedTime:_currentTimeInSeconds];
[self.tableView reloadData];
}
- (IBAction)startCountDown:(id)sender {
if (!_currentTimeInSeconds) {
_currentTimeInSeconds = 0 ;
}
if (!_myTimer) {
_myTimer = [self createTimer];
}
isCurrentlyRunning = YES;
}
- (IBAction)stopCountDown:(id)sender {
if(isCurrentlyRunning == YES){
[_myTimer invalidate];
isCurrentlyRunning = NO;
}else{
if (_myTimer) {
[_myTimer invalidate];
}
_currentTimeInSeconds = 0;
self.lblTimer.text = [self formattedTime:_currentTimeInSeconds];
}
}
在didSelect方法中,在末尾重新加载表视图,将计时器放入选定的行中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[_myTimer invalidate];
self.currentTimeInSeconds = 0;
cellNumber = [indexPath row];
[tblView reloadData] //replace tblView with the name of your tableView, if you don't have an outlet for it then you can create one by control-dragging it to the VC
}
然后
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"SimpleIdentifier"];
UILabel *lblName = (UILabel *)[cell viewWithTag:1010];
UILabel *lblTime = (UILabel *)[cell viewWithTag:1020];
NSString *Name, *Time;
Name = [self.roundOrder objectAtIndex:indexPath.row];
if (indexPath.row == cellNumber) {
lblTime.text = [self formattedTime:self.currentTimeInSeconds];
} else {
lblTime.text = //whatever you want it to say in the rows without the timer
}
lblName.text = Name;
return cell;
}
我想这就是你要问的,如果是的话,那就应该这样做
计时器在每行中显示的原因是您将计时器设置为每个单元格中的lblTime;它没有任何限制,因此对于每个indexath.row,它都会显示计时器谢谢!那句简单的if语句让一切变得不同!你很聪明!我唯一改变的是,我将重新加载的数据放在TimerChecked方法中,因为否则它不会显示。非常感谢你!!!没问题。很高兴能帮上忙嘿,卢卡斯!我注意到了!当我们从一个细胞移动到另一个细胞时,我们还能保留我们放在细胞中的数据吗?@Ares你是什么意思?是否保留计时器上剩余的时间?保留已使用的单元格?事实上,我之前所说的方式是错误的…我很确定我有内存泄漏错误…每当我在某个单元格中运行计时器时,时间标签也会出现在另一个随机单元格中!我不知道是什么原因造成的…我应该再发一个问题吗?