gpt4 book ai didi

ios - indexPath.row 给出了奇怪的值

转载 作者:行者123 更新时间:2023-12-01 20:00:08 25 4
gpt4 key购买 nike

我有一个自定义单元格。在 cellForRowAtIndexPath我正在显示的方法indexPath.row自定义编号 UILabel也在 cellForRowAtIndexPath 中定义.该标签被添加为该自定义单元格的 subview 。当我有很多单元格并向下滑动表格 View 时,indexPath.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;
}

最佳答案

避免在 cellForRow 方法中创建新的 UIButtons 和 UILabels。 UITableView 中的单元格被重用,因此当您滚动时从屏幕上消失的对象再次用于显示新行。每次发生这种情况时,您都会添加一个新 View 。

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

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

//update subviews here

关于ios - indexPath.row 给出了奇怪的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40193056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com