1

I have a table with 7 section and in each section 2 rows, I don't know why my delete button doesn't work!! and not appear in cells

would you please help me! Thanks in advance!

enter image description here

day.m

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    arry = [[NSMutableArray alloc] init];
    [arry addObject:@"test"];
    [arry addObject:@"hey"];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

-(void)addNewItem
{
    [arry addObject:@"New Day"];
    [self.tableView reloadData];
}

// ... standard methods to override

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 7;
    //_week.days.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [arry count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.text = [arry objectAtIndex:indexPath.row];
    return cell;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return @"Monday";
    }
    else if(section == 1)
    {
        return @"Tuesday";
    }
    else if(section == 2)
    {
        return @"Wednesday";
    }
    else if(section == 3)
    {
        return @"Thuesday";
    }
    else if(section == 4)
    {
        return @"Friday";
    }
    else if(section == 5)
    {
        return @"Saturday";
    }
    else
    {
        return @"Sunday";
    }
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // [self.monthTitle removeObjectAtIndex:indexPath.row];
        [arry removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];
    }
    else if (editingStyle==UITableViewCellEditingStyleInsert)
    {   
    }   
}

Edit1: after removing multiple selection from storyboard delete button appears but when I click on delete button it's terminated

enter image description here

4

3 回答 3

1

你没有开始你的表更新。当您使用方法“deleteRowsAtIndexPath”时,您必须在这些方法之间插入它:

// Begin update
[tableView beginUpdates];

// Perform update
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                 withRowAnimation:UITableViewRowAnimationFade];

// End update
[tableView endUpdates]
于 2012-07-27T10:44:12.433 回答
1

从您的屏幕截图中,您似乎已设置allowsMultipleSelectionDuringEditingYES(可能在您的 nib 文件中,因为我在代码中看不到)。在这种情况下,tableView:commitEditingStyle:forRowAtIndexPath:将永远不会被调用,因为复选标记仅表示用户的选择,而不是应该删除这些行。

您需要一个自己的删除按钮,当您进入编辑模式时该按钮会被激活,类似于您在邮件应用程序中看到的方式。

于 2012-07-27T11:34:59.987 回答
0

您应该首先像这样从数据源中删除行,

            [self.table_favpropertylist_array removeObjectAtIndex:indexPathRow];
            [tbl_FavDetails beginUpdates];

            UITableViewCell *cell=[tbl_FavDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPathRow inSection:0]];

            NSIndexPath *indexPath = [tbl_FavDetails indexPathForCell:cell];
            [tbl_FavDetails deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                  withRowAnimation:UITableViewRowAnimationFade];
            [tbl_FavDetails endUpdates];

其中 self.table_favpropertylist_array 是数据源,tbl_FavDetails 是 tableview,indexPathRow 是单击行删除按钮的索引,

谢谢。

于 2013-07-31T08:17:41.560 回答