Ios 行给出了奇怪的值

Ios 行给出了奇怪的值,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个定制的手机。在cellforrowatinexpath方法中,我将indexPath.row编号显示为自定义UILabel,也在cellforrowatinexpath中定义。该标签将作为该自定义单元格的子视图添加。当我有很多单元格并向下滑动tableview时,那indexath.row数字有奇怪的值,如果我滚动更多,值就会变大。我怎么能绕过这个 [编辑]我的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cell

我有一个定制的手机。在
cellforrowatinexpath
方法中,我将
indexPath.row
编号显示为自定义
UILabel
,也在
cellforrowatinexpath
中定义。该标签将作为该自定义单元格的子视图添加。当我有很多单元格并向下滑动tableview时,那
indexath.row
数字有奇怪的值,如果我滚动更多,值就会变大。我怎么能绕过这个

[编辑]我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SKSTableViewCell";

    self.sksTableViewCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!self.sksTableViewCell)
        self.sksTableViewCell = [[SKSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    self.sksTableViewCell.textLabel.text = [NSString stringWithFormat:@"  %@",[[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] name]];
    self.sksTableViewCell.backgroundColor = [UIColor clearColor];

    if ([[[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] times] count]) {
        self.sksTableViewCell.expandable = YES;
    } else {
        self.sksTableViewCell.expandable = NO;
    }

    // UIButton for directly switching racers
    UIButton *stopwatchIcon = [UIButton buttonWithType:UIButtonTypeCustom];
    [stopwatchIcon addTarget:self action:@selector(pressedButtonToGoToTheRacerDirectly:) forControlEvents:UIControlEventTouchUpInside];
    UIImage *img = [UIImage imageNamed:@"stopwatch"];
    stopwatchIcon.frame = CGRectMake(self.view.bounds.size.width-70, 27.5-img.size.height/2, img.size.width, img.size.height);
    [stopwatchIcon setImage:img forState:UIControlStateNormal];
    stopwatchIcon.tag = [[self.indexDictionary valueForKey:[NSString stringWithFormat:@"%@", [[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] name]]] intValue];
    [self.sksTableViewCell addSubview:stopwatchIcon];

    // Medals
    int golds = [[self.bestThreeRacersDictionary objectForKey:@"golds"] intValue];
    int silvers = [[self.bestThreeRacersDictionary objectForKey:@"silvers"] intValue];
    int bronzes = [[self.bestThreeRacersDictionary objectForKey:@"bronzes"] intValue];

    // Medal image and view
    UIImage *medalImage = [UIImage imageNamed:@"medal"];
    UIImageView *medalImageView = [[UIImageView alloc] initWithFrame:CGRectMake(stopwatchIcon.frame.origin.x-medalImage.size.width-7.5, 27.5-medalImage.size.height/2, medalImage.size.width, medalImage.size.height)];
    // TextLabel
    UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(medalImageView.frame.origin.x+medalImageView.frame.size.width/4, medalImageView.frame.origin.y+medalImageView.frame.size.height/4, medalImageView.frame.size.width/2, medalImageView.frame.size.height/2)];
    numberLabel.backgroundColor = [UIColor clearColor];
    numberLabel.textColor = [UIColor whiteColor];
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.adjustsFontSizeToFitWidth = YES;
    numberLabel.font = [UIFont systemFontOfSize:12];
    numberLabel.text = [NSString stringWithFormat:@"%i", [Racer shift:golds silvers:silvers bronzes:bronzes]+helpIndex];
    helpIndex++;
    if (indexPath.row < golds) {
        medalImage = [UIImage imageNamed:@"goldMedal"];
        numberLabel.text = @"1";
        helpIndex--;
    } else if (indexPath.row < (golds+silvers)) {
        medalImage = [UIImage imageNamed:@"silverMedal"];
        numberLabel.text = @"2";
        helpIndex--;
    } else if (indexPath.row < (golds+silvers+bronzes)) {
        medalImage = [UIImage imageNamed:@"bronzeMedal"];
        numberLabel.text = @"3";
        helpIndex--;
    }
    medalImageView.image = medalImage;
    medalImageView.tag = 5;
    numberLabel.tag = 5;
    [self.sksTableViewCell addSubview:medalImageView];
    [self.sksTableViewCell addSubview:numberLabel];

    return self.sksTableViewCell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“SKSTableViewCell”;
self.sksTableViewCell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(!self.skstablevewcell)
self.sksTableViewCell=[[sksTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
self.sksTableViewCell.textLabel.text=[NSString stringWithFormat:@“%@”,[[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row]name]];
self.sksTableViewCell.backgroundColor=[UIColor clearColor];
如果([[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row]次]计数]){
self.skstablevewcell.expandable=是;
}否则{
self.skstablevewcell.expandable=否;
}
//用于直接切换赛车手的UI按钮
UIButton*StopWatch图标=[UIButton按钮类型:UIButtonTypeCustom];
[秒表图标添加目标:自我操作:@选择器(按按钮直接转到其他Acer:)用于控制事件:UIControlEventTouchUpInside];
UIImage*img=[UIImage ImageName:@“秒表”];
stopwatchIcon.frame=CGRectMake(self.view.bounds.size.width-70,27.5-img.size.height/2,img.size.width,img.size.height);
[秒表图标设置图像:img for状态:uicontrol状态正常];
stopwatchIcon.tag=[[self.indexDictionary valueForKey:[NSString stringWithFormat:@“%@”,[[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row]name]]intValue];
[self.sksTableViewCell addSubview:stopWatch图标];
//奖牌
int golds=[[self.bestThreeRacerDictionary objectForKey:@“golds”]intValue];
int silvers=[[self.bestThreeRacerDictionary objectForKey:@“silvers”]intValue];
int brownzes=[[self.bestThreeRacerDictionary objectForKey:@“brownzes”]intValue];
//奖牌形象与观点
UIImage*medalImage=[UIImage ImageName:@“奖牌”];
UIImageView*medalImageView=[[UIImageView alloc]initWithFrame:CGRectMake(秒表图标.框架.原点.x-medalImage.size.WITH-7.5,27.5-medalImage.size.height/2,medalImage.size.WITH,medalImage.size.HEIGH)];
//文本标签
UILabel*numberLabel=[[UILabel alloc]initWithFrame:CGRectMake(medalImageView.frame.origin.x+medalImageView.frame.size.width/4,medalImageView.frame.origin.y+medalImageView.frame.size.width/2,medalImageView.frame.size.height/2)];
numberLabel.backgroundColor=[UIColor clearColor];
numberLabel.textColor=[UIColor-whiteColor];
numberLabel.textAlignment=NSTextAlignmentCenter;
numberLabel.adjustsFontSizeToFitWidth=是;
numberLabel.font=[UIFont systemFontOfSize:12];
numberLabel.text=[NSString stringWithFormat:@“%i”,[Racer shift:golds silvers:silvers bronzes:bronzes]+helpIndex];
helpIndex++;
if(indexPath.row
避免在cellForRow方法中创建新的UIButton和UILabel。UITableView中的单元格被重用,因此当您滚动时从屏幕上消失的对象将再次用于显示新行。每次发生这种情况时,您都会添加一个新视图

相反,您应该只创建一次所有视图层次结构,并更新需要更新的视图

if (!cell) {
    cell = [[SKSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    //create all your subviews here
}

//update subviews here

不要为每个单元创建新的UI组件对象,只需在单元重用后再创建一次即可

你能给我们看一下你的代码吗?也许每次调用
cellForRow
时你都在添加一个新标签?如果是这样,您不应该这样做。为什么要使用属性保留对最后一个单元格的引用?