如何在 iPhone 中编辑 TableView?

发布于 2024-11-06 01:26:59 字数 141 浏览 5 评论 0原文

在我的应用程序中,我的表格视图需要一个编辑按钮,可以删除一行或可以更改其位置。 当我使用默认导航栏时,这真的很容易,但现在就我而言,我使用的是感染图像视图和图像视图的自定义栏。现在我需要一个可以编辑表格视图的按钮。我没有使用默认的导航栏。

所以请帮助我

In my application i need an edit button for my tableview that can delete a row or can change its position.
it is really easy when i am using a default navigation bar but now in my case i am using a custom bar that is infect an imageview & now i need a button that can edit a tableview. i am not using the default navigation bar.

so plz help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雄赳赳气昂昂 2024-11-13 01:26:59

你只需要设置 UITableView 的 editing 属性,在你的 UITableViewController 中实现类似的东西;

- (void) editingButtonPressed:(id)sender {
  if([self isEditing]) {
    [sender setText:@"Edit" forState:UIControlStateNormal];
    [self setEditing:NO animated:YES];
  } else {
    [sender setText:@"Done" forState:UIControlStateNormal];
    [self setEditing:YES animated:YES];
  }
}

并将其连接到您的按钮或图像,如果没有文本,请将 setText 替换为 setImage

这是我使用自定义 UIToolbarUITableViewControllerinit 方法,该方法将两个按钮添加到导航栏以代替右侧导航栏按钮。

- (id) init {

    [super initWithStyle:UITableViewStyleGrouped];

    UIBarButtonItem *email = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Email.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(composeEmail:)] autorelease];
    UIBarButtonItem *bookmark = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBookmark:)] autorelease];
    [bookmark setStyle:UIBarButtonItemStyleBordered];

    CustomToolbar *buttonToolbar = [[CustomToolbar alloc] initWithFrame:CGRectMake(0, 0, 93, 45)];
    [buttonToolbar setBarStyle:UIBarStyleBlackTranslucent];
    [buttonToolbar setItems:[NSArray arrayWithObjects:email, bookmark, nil] animated:NO];

    [[self navigationItem] setTitle:@"Table with Custom Toolbar"];
    [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:buttonToolbar] autorelease]];

    [buttonToolbar release];

    return self;
}

创建按钮时,我在创建按钮时使用 action:@selector(customMethodName) 将我的方法连接到按钮操作,在本例中为 composeEmailaddBookmark< /code> 加载这些任务的新视图。

You just need to set the editing property of the UITableView, in your UITableViewController implement something like;

- (void) editingButtonPressed:(id)sender {
  if([self isEditing]) {
    [sender setText:@"Edit" forState:UIControlStateNormal];
    [self setEditing:NO animated:YES];
  } else {
    [sender setText:@"Done" forState:UIControlStateNormal];
    [self setEditing:YES animated:YES];
  }
}

And hook that up to your button or image and replace the setText with setImage if you have no text.

Here is my init method for a UITableViewController using a custom UIToolbar which adds two buttons to the navigation bar in place of the right navigation bar button.

- (id) init {

    [super initWithStyle:UITableViewStyleGrouped];

    UIBarButtonItem *email = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Email.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(composeEmail:)] autorelease];
    UIBarButtonItem *bookmark = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBookmark:)] autorelease];
    [bookmark setStyle:UIBarButtonItemStyleBordered];

    CustomToolbar *buttonToolbar = [[CustomToolbar alloc] initWithFrame:CGRectMake(0, 0, 93, 45)];
    [buttonToolbar setBarStyle:UIBarStyleBlackTranslucent];
    [buttonToolbar setItems:[NSArray arrayWithObjects:email, bookmark, nil] animated:NO];

    [[self navigationItem] setTitle:@"Table with Custom Toolbar"];
    [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:buttonToolbar] autorelease]];

    [buttonToolbar release];

    return self;
}

When creating the buttons I use action:@selector(customMethodName) when creating the buttons to hook up my methods to the button actions in this case composeEmail and addBookmark which load the new views for those tasks.

尝蛊 2024-11-13 01:26:59

您可以在图像视图上添加一个按钮,然后单击该按钮,将表视图编辑设置为“是”。

这是如何添加图像视图和按钮的代码:-

UIImageView *imageView2=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    [imageView2 setImage:[UIImage imageNamed:@"bottom bar_gda.png"]];
    [self.view addSubview:imageView2];
    [self.view bringSubviewToFront:imageView2];
    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setFrame:CGRectMake(280, 3, 26, 36)];
    deleteButton.contentMode = UIViewContentModeScaleAspectFill;     
    UIImage *newImage12 = [UIImage imageNamed:@"check.png"];
    [deleteButton setBackgroundImage:newImage12 forState:UIControlStateNormal];
    [deleteButton setBackgroundImage:newImage12 forState:UIControlStateHighlighted];
    [deleteButton addTarget:self action:@selector(editmethod:) forControlEvents:UIControlEventTouchUpInside];
    [imageView2 addSubview:deleteButton]; 

you can add a button on your imageview and on that button click set your tableviewediting to yes.

here's the code how to add a imageview and button:-

UIImageView *imageView2=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    [imageView2 setImage:[UIImage imageNamed:@"bottom bar_gda.png"]];
    [self.view addSubview:imageView2];
    [self.view bringSubviewToFront:imageView2];
    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setFrame:CGRectMake(280, 3, 26, 36)];
    deleteButton.contentMode = UIViewContentModeScaleAspectFill;     
    UIImage *newImage12 = [UIImage imageNamed:@"check.png"];
    [deleteButton setBackgroundImage:newImage12 forState:UIControlStateNormal];
    [deleteButton setBackgroundImage:newImage12 forState:UIControlStateHighlighted];
    [deleteButton addTarget:self action:@selector(editmethod:) forControlEvents:UIControlEventTouchUpInside];
    [imageView2 addSubview:deleteButton]; 
过期以后 2024-11-13 01:26:59

您必须将 UIButton 实例添加到该 imageview 并实现一个方法,该方法将在该 UIButton 上发生事件时执行。

该方法的代码如下所示,

if(tableView.editing)
[tableView setEditing:NO animated:YES]
else
[tableView setEditing:YES animated:YES]

阅读 UITableView文档以获取更多信息。

You have to add UIButton instance to that imageview and implement a method that will execute when an event occurs on that UIButton.

Code for the method will look like,

if(tableView.editing)
[tableView setEditing:NO animated:YES]
else
[tableView setEditing:YES animated:YES]

Read UITableView Documentation for more info.

我三岁 2024-11-13 01:26:59

您可以使用滑动来删除功能。
这可以通过以下方式完成。

你需要实现以下3个tableView的委托方法。

tableView:commitEditingStyle:forRowAtIndexPath:
tableView:canEditRowAtIndexPath:
tableView:editingStyleForRowAtIndexPath: 

在方法中

tableView:editingStyleForRowAtIndexPath:

返回类型应该是

UITableViewCellEditingStyleDelete.

YOU CAN USE SWIPE TO DELETE FEATURE.
THIS CAN BE DOEN IN FOLLOWING WAY.

you need to implement following 3 delegate method of tableView.

tableView:commitEditingStyle:forRowAtIndexPath:
tableView:canEditRowAtIndexPath:
tableView:editingStyleForRowAtIndexPath: 

In method

tableView:editingStyleForRowAtIndexPath:

RETURN TYPE SHOULD BE

UITableViewCellEditingStyleDelete.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文