gpt4 book ai didi

ios - 如何在给定不同排序参数的情况下自动重绘 UITableViewCells

转载 作者:行者123 更新时间:2023-11-29 02:40:17 26 4
gpt4 key购买 nike

我有一个名为 HighScoreViewController 的 UITableViewController,它拥有 UITableViewCells,每个都是单独的 HighScore 对象。

当我按下顶部的分段控件时,我想立即重新使用 TableCells。但是,目前我的排序只能通过拖动来实现,这样单元格就会离开屏幕,从而导致 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 再次被调用(根据分数、持续时间或日期对单个单元格进行排序)。

我尝试创建一个方法,该方法将在我的分段控制操作中调用,该方法将删除和添加 View 以重绘单元格。当这不起作用时,我尝试手动重绘每个单元格,但我意识到这将非常复杂,因为我必须监视哪些分数已经在 View 中排序以及哪些分数在数据源中排序。我还尝试了 setNeedsDisplay

如何在按下分段控件后立即对 UITableViewCells 进行求值?

HighScoreVC HSVCduration

这是分段控件的 Action 导出

- (IBAction)changeSort:(id)sender {


NSLog(@"changing Sort");
// Sorted by score
if (self.sortingButton.selectedSegmentIndex == SORT_BY_SCORE){
[self sortHighScoresByScore];
}else if (self.sortingButton.selectedSegmentIndex == SORT_BY_DURATION){// sorted by duration
[self sortHighScoresByDuration];
}else if (self.sortingButton.selectedSegmentIndex == SORT_BY_DATE){// sorted by last time played
[self sortHighScoresByDate];
}else{
NSLog(@"HighScoreViewController, changeSort else != 0,1,2");
}
}

这是我的单元格方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HighScoreCell" forIndexPath:indexPath];
NSLog(@"inside TableView");

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"HighScoreCell"];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *playingCardHighScores = [defaults objectForKey:@"playingCardHighScores"];
NSMutableArray *setCardHighScores = [defaults objectForKey:@"setCardHighScores"];

// Decode High Scores arrays
playingCardHighScores = [self decodeEncodedArray:playingCardHighScores];
setCardHighScores = [self decodeEncodedArray:setCardHighScores];

/* Will need to set the left and right side to two different scores or add some kind of slider*/
HighScore *hs = playingCardHighScores[indexPath.row];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(55, 4, 260, 20)];
label.font = [UIFont boldSystemFontOfSize:15.0];
label.tag=25;

UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 260, 15)];
detailLabel.adjustsFontSizeToFitWidth = YES;
detailLabel.tag=30;

NSString *score = [NSString stringWithFormat:@"Score:%ld",(long)hs.score];
NSString *duration = [NSString stringWithFormat:@",Duration:%.2f",hs.secondsPlayed];
NSString *datePlayed = [NSString stringWithFormat:@",datePlayed:%@",hs.datePlayed];
NSString *combination = [score stringByAppendingString:[duration stringByAppendingString:datePlayed]];
detailLabel.text = combination;

[cell.contentView addSubview:label];
[cell.contentView addSubview:detailLabel];

// Should change depending on what selector is picked
NSString *strScore;
NSDate *date;
switch (self.sortingButton.selectedSegmentIndex) {
case SORT_BY_SCORE:
strScore = [NSString stringWithFormat:@"Score: %ld",(long)hs.score];
break;
case SORT_BY_DURATION:
strScore = [NSString stringWithFormat:@"Duration: %.2f seconds",hs.secondsPlayed];
break;
case SORT_BY_DATE:
date = hs.datePlayed;
break;
default:
break;
}
// if the string exists
if (strScore){
cell.textLabel.text = strScore;
}else{
cell.textLabel.text = [date description];
}
return cell;
}

最佳答案

您需要在分段控件 IBAction 中调用 [self.tableView reloadData] 以重新创建屏幕上的所有单元格。

或者,如果您希望能够指定股票动画,您可以在 TableView 上调用 reloadSections:withRowAnimation:

关于ios - 如何在给定不同排序参数的情况下自动重绘 UITableViewCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25879392/

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