我在谷歌搜索了很多次,但无法找到我想要的内容。 我在表格视图中有一个自定义单元格,其中有三个标签和一个删除按钮。现在我想删除我单击删除按钮的特定行。所以,我只想知道在删除按钮的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 :(得分: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];
}
答案 1 :(得分: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
,则会有删除动画。
答案 2 :(得分:0)
在Tableview的cellForRowAtIndexPath
函数中,为删除按钮设置tag
属性。
<DELETE_BUTTON>.tag = indexPath.row
。
然后在删除功能中,您将获得单击的索引。
删除该索引中的对象并重新加载表。
- (IBAction)deleteButtonTapped:(id)sender {
int index = [sender tag];
[self.arrPeopleDetail removeObjectAtIndex:index];
[self.detailTable reloadData];
}
答案 3 :(得分: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];
}