【问题标题】:moveRowAtIndexPath doesn't get calledmoveRowAtIndexPath 没有被调用
【发布时间】:2014-01-11 10:49:58
【问题描述】:

我的代码没有进入 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];
        }
    }
}

【问题讨论】:

    标签: ios objective-c uitableview ios7


    【解决方案1】:
     - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
    

    Minus在上层方法返回YES时出现

    在方法中

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

    您允许将单元格从 1 indexPath 移动到另一个 indexPath。 你应该是这个 tableView 的委托和数据源

    【讨论】:

    • 出现减号,但是当我点击减号时没有出现“删除”。
    • - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; }
    • - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;在这个方法中设置属性 cell.editingStyle = UITableViewCellEditingStyleDelete;
    • 我无法使用 cell.editingStyle = UITableViewCellEditingStyleDelete ,它给我错误,并且如果我将返回值设为 UITableVeiwCellEditingStyleDelete,则编辑样式的 deleaget 方法也不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 2019-03-31
    • 2012-12-16
    • 2015-03-28
    相关资源
    最近更新 更多