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

标签 ios objective-c iphone uitableview

我有一个名为 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/

相关文章:

ios - 无法根据 ios 中的标签文本更改正确的 UITableViewCell 高度

iphone - nslog的iPhone控制台?

ios - 修复 Leptonica 1.68 中的局部偏斜

iphone - 在 iPhone 上检查字符串是否为整数的更好方法是什么?

objective-c - 向左填充字符串

ios - 如何使用 Swift 在 IOS 中使用自定义标题操作在 tableview 中显示 Collection View

ios - 在表格 View 单元格上单击打开 URL

ios - 以编程方式在 UITableViewCell 内的 UICollectionView 内自定义 UICollectionViewCell

ios - 禁用 Zombies 会导致 EXC_BAD_ACCESS

objective-c - 使用 prepareForSegue 作为按钮