移动单元格行然后访问它们的属性时,我遇到了一个非常奇怪的故障。老实说,这太难解释了,所以我记录了 iOS 模拟器上发生的故障。我没有使用故事板。
这是视频: iPhone故障
以下是一些相关代码:
表视图控制器.m:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
NSString *detailText = [NSString stringWithFormat:@"%.0f", [[taskArray objectAtIndex:[indexPath row]] timeInterval]];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
[cell setBackgroundColor:[UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f]];
cell.textLabel.textColor = turquoiseColor;
[[cell detailTextLabel] setText:detailText];
[[cell detailTextLabel] setFont:[UIFont fontWithName:@"Avenir-Black" size:12]];
[[cell textLabel] setFont:[UIFont fontWithName:@"AvenirNext-DemiBold" size:16]];
if([indexPath section] == 0){
cell.textLabel.text = [[[taskArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
} else if ([indexPath section] == 1) {
cell.textLabel.text = [[[completedArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Tasks *task = [[Tasks alloc]init];
task.taskName = [[taskArray objectAtIndex:[indexPath row]] taskName];
task.timeInterval = [[taskArray objectAtIndex:[indexPath row]] timeInterval];
task.dateCreated = [[taskArray objectAtIndex:[indexPath row]] dateCreated];
DetailViewController *dvc = [[DetailViewController alloc]init];
[dvc setTestTask:task];
[[self navigationController] pushViewController:dvc animated:YES];
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
Tasks *t = [[Tasks alloc]init];
if (sourceIndexPath.row > destinationIndexPath.row) {
[taskArray insertObject:t atIndex:destinationIndexPath.row];
[taskArray removeObjectAtIndex:(sourceIndexPath.row + 1)];
}
else if (sourceIndexPath.row < destinationIndexPath.row) {
[taskArray insertObject:t atIndex:(destinationIndexPath.row + 1)];
[taskArray removeObjectAtIndex:(sourceIndexPath.row)];
}
}
DetailViewController.h(单元格的属性)
@class Tasks;
@interface DetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *timeLeft;
@property (weak, nonatomic) IBOutlet UILabel *timerLabel;
@property (nonatomic, strong) Tasks *testTask;
@end
细节视图控制器.m
- (void)viewDidLoad
{
[super viewDidLoad];
[_timeLeft setFont:[UIFont fontWithName:@"BebasNeue" size:25]];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSString *time = [NSString stringWithFormat:@"%.0f", [testTask timeInterval]];
[_timerLabel setText:time];
[_timerLabel setFont:[UIFont fontWithName:@"BebasNeue" size:60]];
[[self navigationItem] setTitle:[testTask taskName]];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[testTask setTaskName:[testTask taskName]];
[testTask setTimeInterval:[testTask timeInterval]];
}