[英]Wrong indexPath.row number on reusable cells in TableView
如您在下面的代碼中看到的,我有一個tableView
來查看賽車手的時間。 第一名擁有金圈(金goldMedal
形象),第二名擁有銀牌,第三名擁有銅牌,其他所有人只有medal
形象。 這些圖像只是金/銀/青銅/黑眼圈。 我想在此獎牌圖像上放置一個UILabel
視圖,該數字將描述種族在比賽中的raecer的順序,因此,最佳(相同)時間的賽車手將擁有編號為1的goldMedal
圖像,第二名賽車手將獲得帶有silverMedal
圖像的數字2號,依此類推。 問題是,當我在表視圖中有太多賽車手並且向下滾動時,賽車手沒有相同的數字。 滾動時這些數字正在改變。 我不知道該怎么辦。 誰能幫我?
我還使用了helpIndex
變量來為4或更差的賽車手計數,但是現在有indexPath.row
並且這兩種方式都沒有起作用。 誰能至少給我他的觀點,他將如何解決這個問題? 謝謝
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SKSTableViewCell";
SKSTableViewCell *sksTableViewCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(sksTableViewCell.frame.origin.x+22.5, sksTableViewCell.frame.origin.y+20.5, 15, 15)];
numberLabel.font = [UIFont systemFontOfSize:12];
numberLabel.textColor = [UIColor whiteColor];
numberLabel.textAlignment = NSTextAlignmentCenter;
numberLabel.adjustsFontSizeToFitWidth = YES;
if (!sksTableViewCell) {
sksTableViewCell = [[SKSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
sksTableViewCell.backgroundColor = [UIColor clearColor];
if ([[[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] times] count]) {
sksTableViewCell.expandable = YES;
} else {
sksTableViewCell.expandable = NO;
}
}
sksTableViewCell.imageView.image = [UIImage imageNamed:@"medal"];
// Medals
int golds = [[self.bestThreeRacersDictionary objectForKey:@"golds"] intValue];
int silvers = [[self.bestThreeRacersDictionary objectForKey:@"silvers"] intValue];
int bronzes = [[self.bestThreeRacersDictionary objectForKey:@"bronzes"] intValue];
sksTableViewCell.textLabel.text = [NSString stringWithFormat:@" %@",[[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] name]];
numberLabel.text = [NSString stringWithFormat:@"%lu", [Racer shift:golds silvers:silvers bronzes:bronzes]+indexPath.row];
helpIndex++;
if (indexPath.row < golds) {
sksTableViewCell.imageView.image = [UIImage imageNamed:@"goldMedal"];
numberLabel.text = @"1";
helpIndex--;
} else if (indexPath.row < (golds+silvers)) {
sksTableViewCell.imageView.image = [UIImage imageNamed:@"silverMedal"];
numberLabel.text = @"2";
helpIndex--;
} else if (indexPath.row < (golds+silvers+bronzes)) {
sksTableViewCell.imageView.image = [UIImage imageNamed:@"bronzeMedal"];
numberLabel.text = @"3";
helpIndex--;
}
[sksTableViewCell addSubview:numberLabel];
if ([[[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] times] count]) {
sksTableViewCell.expandable = YES;
} else {
sksTableViewCell.expandable = NO;
}
sksTableViewCell.textLabel.text = [NSString stringWithFormat:@"%@", [[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] name]];
return sksTableViewCell;
}
不必在cellForRowAtIndexPath中一遍又一遍地添加numberLabel,而是將其添加到您的SKSTableViewCell中:
UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(sksTableViewCell.frame.origin.x+22.5, sksTableViewCell.frame.origin.y+20.5, 15, 15)];
numberLabel.font = [UIFont systemFontOfSize:12];
numberLabel.textColor = [UIColor whiteColor];
numberLabel.textAlignment = NSTextAlignmentCenter;
numberLabel.adjustsFontSizeToFitWidth = YES;
該問題應得到解決。
sksTableViewCell = [[SKSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
您應該在此處傳遞CellIdentifier
,否則將無法檢查單元格是否存在。 UITableView
將始終創建新的單元格。
我的一些筆記可能會幫助您:
如果願意,可以使用其他可重復使用的標識符參數,並根據標識符區分單元格定義內的單元格。
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
您可以為表單元格的每個子類分別為Gold,Silver,Bronze和black創建四個不同的類,並定義它們中的所有內容,然后根據需要簡單地調用它們。 這將減少您的開銷,因為您不會在上下滾動時一遍又一遍地執行此工作。 另外,將來如果您需要為每個單元做更多具體的工作,這對您來說將非常方便。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.