我的问题与此线程中的问题类似(我认为): Cell shows on top of section header
但是,我已尝试该线程中的所有内容,并且只有异常错误。
这就是我的问题:
iPhone Problem Video
以下是我的TableViewController中的一些相关方法:
TableViewController.h
@interface ToDoTableViewController : UITableViewController <Properties2ViewControllerDelegate, UITableViewDelegate>{
UIButton *button;
}
@property (strong, nonatomic) NSMutableArray *taskArray;
@property (strong, nonatomic) NSMutableArray *completedArray;
-(IBAction)addCell:(id)sender;
-(void)buttonPressed:(id)sender;
TableViewController.m
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView;
NSString *sectionTitle = @"Incomplete Tasks";
NSString *section2Title = @"Completed Tasks";
UILabel *label = [[UILabel alloc]init];
label.textColor = [UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f];
label.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:25];
label.backgroundColor = [UIColor clearColor];
CGRect buttonFrame = CGRectMake(280.0, 3.0, 30.0, 30.0);
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:buttonFrame];
[button addTarget:self action:@selector(addCell:) forControlEvents:UIControlEventTouchUpInside];
[button.layer setCornerRadius:8.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderWidth:1.0f];
[button setBackgroundColor:[UIColor clearColor]];
[button.layer setBorderColor:[[UIColor colorWithWhite:0.3 alpha:0.7] CGColor]];
button.userInteractionEnabled = YES;
[button setImage:[UIImage imageNamed:@"plus_16.png"] forState:UIControlStateNormal];
switch (section) {
case 0:
label.text = sectionTitle;
label.frame = CGRectMake(10.0, 0.0, 320.0, 40.0);
headerView = [[UIView alloc]initWithFrame:label.frame];
[headerView addSubview:label];
[headerView addSubview:button];
break;
case 1:
label.text = section2Title;
label.frame = CGRectMake(10.0, 60.0, 320.0, 40.0);
headerView = [[UIView alloc]initWithFrame:label.frame];
[headerView addSubview:label];
// if (completedArray == nil)
// headerView.hidden = YES;
break;
}
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
switch (section) {
case 0:
return 40.0;
break;
case 1:
return 40.0;
break;
}
return 0;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
Tasks *t = [[Tasks alloc]init];
if (sourceIndexPath.row > destinationIndexPath.row) {
// insert the object at the target row
[taskArray insertObject:t atIndex:destinationIndexPath.row];
// remove the original from the data structure
[taskArray removeObjectAtIndex:(sourceIndexPath.row + 1)];
}
else if (sourceIndexPath.row < destinationIndexPath.row) { //from <- to
// insert the object at the target row
[taskArray insertObject:t atIndex:(destinationIndexPath.row + 1)];
// remove the original from the data structure
[taskArray removeObjectAtIndex:(sourceIndexPath.row)];
}
}
---编辑---我解决了这个问题:
好吧我首先按照Martin R的建议消除两个部分标题之间的额外空间来解决这个问题。然后我实现了以下方法:
- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (proposedDestinationIndexPath.section != sourceIndexPath.section)
{
//keep cell where it was...
return sourceIndexPath;
}
//ok to move cell to proposed path...
return proposedDestinationIndexPath;
}