2015-02-23 98 views
-1

我已经在谷歌搜索很多次,但无法找到我正在寻找什么。 我在我的表格视图中有一个自定义单元格,其中有三个标签和一个删除按钮。现在我想删除我点击删除按钮的特定行。所以,我只想知道在delete按钮的ibAction方法中写入的代码是什么。提前致谢。如何在单击按钮时删除一个特定行?

这是我ViewController.m

#import "ViewController.h" 
#import "detailObject.h" 

@interface ViewController() 

@end 


@implementation ViewController 


- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view, typically from a nib. 
    self.arrPeopleDetail = [[NSMutableArray alloc]init]; 
} 

- (IBAction)submitButton:(id)sender { 
    if ([self.phoneTextField.text isEqualToString:@""] && [self.addressTextField.text isEqualToString:@""] && [self.phoneTextField.text isEqualToString:@""]) { 
     UIAlertView *alrt = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Fillup The Above Fields" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
     [alrt show]; 
    } 
    else if ([self.nameTextField.text isEqualToString:@""]) { 
     UIAlertView *alrt_name = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Plaeas Enter Name" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil ]; 
     [alrt_name show]; 
    } 
    else if ([self.addressTextField.text isEqualToString:@""]) { 
     UIAlertView *alrt_address = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Enter Address" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
     [alrt_address show]; 
    } 
    else if ([self.phoneTextField.text isEqualToString:@""]) { 
     UIAlertView *alrt_phoneNumber = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Enter Phone Number" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
     [alrt_phoneNumber show]; 
    } 
    else 
    { 
     detailObject *peopleDetail = [[detailObject alloc] init]; 
     peopleDetail.strPeopleName = self.nameTextField.text; 
     peopleDetail.strPeopleAddress = self.addressTextField.text; 
     peopleDetail.strPeoplePhoneNumber = self.phoneTextField.text; 

     [self.arrPeopleDetail addObject:peopleDetail]; 

     self.nameTextField.text = @""; 
     self.addressTextField.text = @""; 
     self.phoneTextField.text = @""; 
    } 
    [self.detailTable reloadData]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"]; 

    UILabel *lbl1 = (UILabel*)[cell.contentView viewWithTag:1]; 
    lbl1.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:@"strPeopleName"]; 

    UILabel *lbl2 = (UILabel*)[cell.contentView viewWithTag:2]; 
    lbl2.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:@"strPeopleAddress"]; 

    UILabel *lbl3 = (UILabel*)[cell.contentView viewWithTag:3]; 
    lbl3.text = [[self.arrPeopleDetail objectAtIndex:indexPath.row]valueForKey:@"strPeoplePhoneNumber"]; 

    return cell; 
} 

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


- (IBAction)deleteButtonTapped:(id)sender { 
    [self.arrPeopleDetail removeLastObject]; 
    [self.detailTable reloadData]; 
} 

@end 
+0

哪里是你的删除按钮放在哪里?在Table视图中单元格? – 2015-02-23 07:26:09

+0

是的,删除按钮放置在我的表格视图单元格中。 – Tirthendu 2015-02-23 07:29:40

回答

1

模式通常是这样的:

-(void)deleteButtonTapped:(id)sender 
{ 
    // ------------------------------------------------ 
    // Sender is your button 
    // 
    // We're finding the coordinate of 
    // where we tap on the screen, relative 
    // to our tableView 
    // ------------------------------------------------ 
    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.tableView]; 

    // ------------------------------------------------ 
    // Now we use this coordinate to find 
    // the row where we tap on our tableView. 
    // ------------------------------------------------ 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint]; 

    // ------------------------------------------------ 
    // now we have the table row (indexPath.row) 
    // we can use it to delete from our data source 
    // ------------------------------------------------ 

    [self.arrItems removeObjectAtIndex:indexPath.row]; 

    // refresh inteface after updating data source 
    [self.tableView reloadData]; 
} 
+0

我还没有理解前两行正在做什么,但代码正在工作正是我正在寻找。谢谢张。 :) – Tirthendu 2015-02-23 07:49:53

+0

我添加了前两行代码的解释。 – Zhang 2015-02-23 07:54:20

+0

非常感谢。 :) – Tirthendu 2015-02-23 07:55:20

0

首先,deleteButtonTapped:需要知道哪个小区的删除按钮的指数被按下。将按钮的标签设置为tableView:cellForRowAtIndexPath:。例如,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"]; 
    [cell.deleteButton setTag:indexPath.row]; 
    [cell.deleteButton add target:@selector(deleteButtonTapped:) for state : UIControlStateTouchupinside]; 
    return cell; 
} 

现在,您可以从deleteButtomTapped:获得行的索引。

- (IBAction)deleteButtonTapped:(UIButton*)sender { 

    [self.arrPeopleDetail removeObjectAtIndex:sender.tag]; 
    [self.detailTable reloadData]; 
} 

如果使用deleteRowsAtIndexPaths withRowAnimation:而不是reloadData,你将不得不删除动画。

+0

你错过了主行** deleteButtonTapped **在索引 – 2015-02-23 07:30:45

+0

@ Anbu.Karthik行的单元格我假设按钮的目标是通过xib或故事板设置的,因为该方法的返回类型是'IBAction'。 – Ryan 2015-02-23 07:32:18

+0

诡计,当我设置按钮的标记时出现错误,它显示“在UITableViewCell类型的对象上找不到属性'deleteButton'”。 – Tirthendu 2015-02-23 07:38:40

0

cellForRowAtIndexPath函数Tableview中,设置tag属性为删除按钮。 <DELETE_BUTTON>.tag = indexPath.row。 然后在删除功能,你会得到点击索引。 删除该索引中的对象并重新加载表。

- (IBAction)deleteButtonTapped:(id)sender { 
int index = [sender tag]; 
[self.arrPeopleDetail removeObjectAtIndex:index]; 
[self.detailTable reloadData]; 

} 
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tblObj dequeueReusableCellWithIdentifier:@"myCell"]; 

    if (cell==nil) 
    { 
     NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"myCell" owner:self 
               options:nil]; 
     cell = [arr objectAtIndex:0]; 
    } 

    UILabel *lbl1 = (UILabel *)[cell viewWithTag:1]; 
    lbl1.text = [arr1 objectAtIndex:indexPath.row]; 


    UIButton *btn = (UIButton *)[cell viewWithTag:2]; 
    btn.tag = indexPath.row; 

    [btn addTarget:self action:@selector(btnPress:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 
} 

-(IBAction)btnPress:(UIButton *)btn 
{ 
    [arr1 removeObjectAtIndex:btn.tag]; 

    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:btn.tag inSection:0]; 
    [tblObj beginUpdates]; 
    [tblObj deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    [tblObj endUpdates]; 
    [tblObj reloadData]; 

} 
相关问题