我有一个自定义单元格。在cellForRowAtIndexPath
方法中,我将indexPath.row
号码显示为UILabel
中定义的自定义cellForRowAtIndexPath
。该标签将添加为该自定义单元格的子视图。当我有很多单元格并向下滑动tableview时,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;
}
答案 0 :(得分:1)
避免在cellForRow方法中创建新的UIButtons和UILabel。 UITableView中的单元格被重用,因此滚动时从屏幕上消失的对象将再次用于显示新行。而且每次发生这种情况时你都会添加一个新的视图。
相反,您应该只创建一次所有视图层次结构并更新您需要更新的视图。
if (!cell) {
cell = [[SKSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//create all your subviews here
}
//update subviews here
答案 1 :(得分:1)
不要为每个单元格创建UI组件的新对象,然后在单元格重用之后执行一次。