【发布时间】:2014-09-16 22:27:17
【问题描述】:
我有一个名为 HighScoreViewController 的 UITableViewController,它拥有每个单独的 HighScore 对象的 UITableViewCells。
当我按下顶部的分段控件时,我想立即使用 TableCells。但是,目前我的排序只能通过拖动来实现,以便单元格在屏幕外导致再次调用 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath(根据分数、持续时间或日期重新调用单个单元格)。
我尝试创建一个方法,该方法将在我的分段控制操作中调用,该方法将删除和添加视图以重绘单元格。当这不起作用时,我尝试手动重绘每个单元格,但我意识到这将非常复杂,因为我必须监控哪些分数已经在视图中排序以及哪些分数在数据源中排序。我也试过setNeedsDisplay。
如何在按下分段控件后立即使用 UITableViewCells?
这是我对分段控件的操作出口
- (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;
}
【问题讨论】:
标签: ios objective-c iphone uitableview