moveRowAtIndexPath不会被调用

时间:2013-12-23 08:39:28

标签: ios objective-c uitableview ios7

我的代码没有进入moveRowAtIndexPath部分。但它进入了canMoveRowAtIndexPath。

单击减号时,不会显示“删除”。我怎么解决这个问题?谢谢。

ps:我试着让它不用于SearchDisplayController的tableview。

在.h文件中:UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate

      `/*viewdidload*/`       
      self.editbtn addTarget:self action:@selector(toggleEditMode:) forControlEvents:UIControlEventTouchUpInside];
  }
-(void)toggleEditMode:(id)sender {

 if(self.alerjitableview.isEditing){
                [self.alerjitableview setEditing:NO animated:YES];
                [self.editbtn setTitle:@"Düzenle"  forState:UIControlStateNormal];
            }else{
                [self.alerjitableview setEditing:YES animated:YES];
                [self.editbtn setTitle:@"Bitti"  forState:UIControlStateNormal];
            }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    if(tableView== self.alerjitableview)
        return[hastaninalerjileri count];
    else{
        if(tableView == self.searchDisplayController.searchResultsTableView)
            if (isSearching) {
                return [tanilist count];
            } else {
                return [list count];
            }
            else
                return [favTanilar count];
    }
}
- (BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void) tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
       toIndexPath:(NSIndexPath *)targetIndexPath
{
    //if(tableView == self.searchDisplayController.searchResultsTableView)

    if(tableView== self.alerjitableview){
        NSUInteger sourceIndex = [sourceIndexPath row];
        NSUInteger targetIndex = [targetIndexPath row];

        if (sourceIndex != targetIndex)
        {
            [hastaninalerjileri exchangeObjectAtIndex:sourceIndex
                                    withObjectAtIndex:targetIndex];
        }
    }
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        if(tableView== self.alerjitableview){
            [hastaninalerjileri removeObjectAtIndex:[indexPath row]];

            //  Animate deletion
            NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
            [self.alerjitableview deleteRowsAtIndexPaths:indexPaths
                                  withRowAnimation:UITableViewRowAnimationFade];
            [self.alerjitableview reloadData];
        }
        else {
                if (tableView == self.taniView)
                {
                    [hastanintanilari removeObjectAtIndex:[indexPath row]];

                    //  Animate deletion
                    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
                    [self.taniTableView deleteRowsAtIndexPaths:indexPaths
                                               withRowAnimation:UITableViewRowAnimationFade];
                }
        }
    }

}
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0;
}
- (UITableViewCell*)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    TaniModel *taniInfo =[[TaniModel alloc]init];

    if (isSearching && [tanilist count]) {
        taniInfo = [tanilist objectAtIndex:indexPath.row];
    } else {
        taniInfo = [list objectAtIndex:indexPath.row];
    }

    if(tableView == self.alerjitableview){
                OrderAlerjiModel *alerjimodel =[hastaninalerjileri objectAtIndex:indexPath.row];
        cell.textLabel.text = alerjimodel.Ad;
        if((indexPath.row % 2) ==0)
            cell.contentView.backgroundColor=[UIColor colorWithRed:241/256.0 green:237/256.0 blue:237/256.0 alpha:1.0];
        return  cell;
    }
    else{
        if(tableView == self.searchDisplayController.searchResultsTableView){
            cell.textLabel.text = taniInfo.Name;
            cell.textLabel.tag=[NSString stringWithFormat:@"%d",taniInfo.Id];

            if((indexPath.row % 2) ==0)
                cell.contentView.backgroundColor=[UIColor colorWithRed:241/256.0 green:237/256.0 blue:237/256.0 alpha:1.0];

            NSAttributedString *attributedText =[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@ ",cell.textLabel.text ] attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14] }];
            CGRect rect = [attributedText boundingRectWithSize:(CGSize){242/2, MAXFLOAT}
                                                       options:NSStringDrawingUsesLineFragmentOrigin
                                                       context:nil];
            CGSize nameSize = rect.size;

            if (nameSize.height <=25)
                cell.textLabel.numberOfLines=1;
            else if(nameSize.height<=50)
                cell.textLabel.numberOfLines=2;
            else if(nameSize.height<=100)
                cell.textLabel.numberOfLines=3;
            return cell;

        }else{

            FavoriTaniModel *d = [favTanilar objectAtIndex:indexPath.row];
            cell.textLabel.text =d.TaniName;
            cell.tag = [NSString stringWithFormat:@"%d",d.TaniId];

            if((indexPath.row % 2) ==0)
                cell.contentView.backgroundColor=[UIColor colorWithRed:241/256.0 green:237/256.0 blue:237/256.0 alpha:1.0];

            NSAttributedString *attributedText =[[NSAttributedString alloc]initWithString:cell.textLabel.text attributes:@{
                                                                                                                        NSFontAttributeName: [UIFont systemFontOfSize:14]
                                                                                                                        }];
            CGRect rect = [attributedText boundingRectWithSize:(CGSize){242/2, MAXFLOAT}
                                                       options:NSStringDrawingUsesLineFragmentOrigin
                                                       context:nil];
            CGSize nameSize = rect.size;

            cell.textLabel.frame =CGRectMake(30, 0, 242, nameSize.height);
            if (nameSize.height <=25)
                cell.textLabel.numberOfLines=1;
            else if(nameSize.height<=50)
                cell.textLabel.numberOfLines=2;
            else if(nameSize.height<=100)
                cell.textLabel.numberOfLines=3;

            return cell;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == self.taniTableView){
        if(tableView == self.searchDisplayController.searchResultsTableView){

            TaniModel *model=[tanilist objectAtIndex:indexPath.row];

            NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"0",@"Id",@"0",@"OrderId",[NSString stringWithFormat:@"%d",model.Id],@"TaniId",model.Name,@"Ad",@"1",@"OrderBy", nil];
            OrderTaniModel *orderalerji = [[OrderTaniModel alloc]initWithDictionary:dict];

            [hastanintanilari addObject:orderalerji];
            [self.taniTableView reloadData];

            [self.searchDisplayController.searchResultsTableView setHidden:TRUE];
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
在上方法中返回 YES 时出现

减号

方法

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

允许将单元格从1个indexPath移动到另一个indexPath。 你应该成为这个tableView

的委托和数据源