iOS删除一个tableview行 [英] iOS delete a tableview row

查看:126
本文介绍了iOS删除一个tableview行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在iOS 6上正常运行。升级后我停止了从 UITableView 中删除​​行。



我的原型单元格中有一个按钮。



这是按钮的功能:

   - (IBAction)deleteCell:(id)sender {
UIButton * btn =(UIButton *)sender;
UITableViewCell * cell =(UITableViewCell *)btn.superview.superview;
NSIndexPath * indexPath = [_ tableView indexPathForCell:cell];
if([names count]> 9){
int index = indexPath.row;
[names removeObjectAtIndex:index];
[照片removeObjectAtIndex:index];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
}

问题是我的索引变量始终为0. / p>

我尝试了另一种解决方案:

  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if(editingStyle == UITableViewCellEditingStyleDelete){
int index = indexPath。行;
[names removeObjectAtIndex:index];
[照片removeObjectAtIndex:index];
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];

}
}

此解决方案无效。当我向右滑动时没有任何反应。

解决方案

试试这个示例教程,



ViewController.h

  #import< UIKit / UIKit.h> 

@interface ViewController:UIViewController< UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray * arryData1;
NSMutableArray * arryData2;
IBOutlet UITableView * tableList;
}

@end

ViewController.m

  #importViewController.h

@interface ViewController()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

arryData1 = [[NSMutableArray alloc] initWithObjects:@MCA,@MBA,@BTech,@MTech,nil];
arryData2 = [[NSMutableArray alloc] initWithObjects:@Objective C,@C ++,@C#,@。net,nil];
tableList.editing = YES;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arryData1 count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @Cell ;

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [arryData1 objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [arryData2 objectAtIndex:indexPath.row];
返回单元格;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if(editingStyle = = UITableViewCellEditingStyleDelete){
int index = indexPath.row;
[arryData1 removeObjectAtIndex:index];
[arryData2 removeObjectAtIndex:index];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];


}
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableList.editing)
{
返回UITableViewCellEditingStyleDelete;
}

返回UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}


I had my app working fine on iOS 6. With the upgrade I stopped being able to delete rows from my UITableView.

I've a button in my prototype cell.

Here is the button's function:

- (IBAction)deleteCell:(id)sender {
    UIButton *btn = (UIButton*) sender;
    UITableViewCell *cell = (UITableViewCell*) btn.superview.superview;
    NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
    if([names count] > 9) {
        int index = indexPath.row;
        [names removeObjectAtIndex:index];
        [photos removeObjectAtIndex:index];
        [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    } 
} 

The problem is my index variable is always 0.

I tried another solution:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

        if (editingStyle == UITableViewCellEditingStyleDelete) {
            int index = indexPath.row;
            [names removeObjectAtIndex:index];
            [photos removeObjectAtIndex:index];
            [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];

}
}

This solution doesn't work either. Nothing happens when I swipe right.

解决方案

Try this sample tutorial,

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
  NSMutableArray *arryData1;
  NSMutableArray *arryData2;
  IBOutlet UITableView *tableList;
}

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

 @end

@implementation ViewController

-(void)viewDidLoad
{
[super viewDidLoad];

arryData1 = [[NSMutableArray alloc] initWithObjects:@"MCA",@"MBA",@"BTech",@"MTech",nil];
arryData2 = [[NSMutableArray alloc] initWithObjects:@"Objective C",@"C++",@"C#",@".net",nil];
tableList.editing=YES;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arryData1 count];
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text = [arryData1 objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [arryData2 objectAtIndex:indexPath.row];
return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {
    int index = indexPath.row;
    [arryData1 removeObjectAtIndex:index];
    [arryData2 removeObjectAtIndex:index];

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationFade];


}
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableList.editing)
{
    return UITableViewCellEditingStyleDelete;
}

return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

这篇关于iOS删除一个tableview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆