在我的應用程序中,我需要一個用於我的tableview的編輯按鈕,它可以刪除一行或可以更改它的位置。 它是真的很容易,當我使用默認導航欄,但現在在我的情況下,我正在使用感染imageview的自定義欄&現在我需要一個可以編輯tableview的按鈕。我沒有使用默認導航欄。如何在iPhone中編輯TableView?
所以plz幫助我
在我的應用程序中,我需要一個用於我的tableview的編輯按鈕,它可以刪除一行或可以更改它的位置。 它是真的很容易,當我使用默認導航欄,但現在在我的情況下,我正在使用感染imageview的自定義欄&現在我需要一個可以編輯tableview的按鈕。我沒有使用默認導航欄。如何在iPhone中編輯TableView?
所以plz幫助我
你只需要設置的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];
}
}
和掛鉤,截至您的按鈕或圖像,並與setImage
更換setText
,如果您有任何文字。
這是我的init
方法爲UITableViewController
使用自定義UIToolbar
其中導航欄添加兩個按鈕,而不是正確的導航欄按鈕。
- (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;
}
在創建按鈕創建我的按鈕時,連接我的方法,該按鈕的動作在這種情況下composeEmail
和addBookmark
其裝載這些任務的新視圖使用action:@selector(customMethodName)
。
如果你不介意你願意告訴我發件人有什麼價值? – Mashhadi 2011-05-09 08:05:28
@Mashhadi發件人是調用方法的對象,它可能是您的按鈕或圖像對touchUpInside事件的響應。你的自定義欄是否擴展了'UIToolbar'?我已經添加了一些代碼來顯示如何將自定義工具欄添加到我的「UITableViewController」中。 – 2011-05-10 01:43:13
您可以添加您的ImageView和那個按鈕點擊設置你的tableviewediting爲yes按鈕。
這裏的代碼如何添加一個ImageView的和按鈕: -
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];
可以使用滑動刪除特徵。 這可以按照以下方式進行。
你需要實現以下3個tableView的委託方法。
tableView:commitEditingStyle:forRowAtIndexPath:
tableView:canEditRowAtIndexPath:
tableView:editingStyleForRowAtIndexPath:
在方法
tableView:editingStyleForRowAtIndexPath:
返回類型應該是
UITableViewCellEditingStyleDelete.
您必須將UIButton實例添加到該圖像視圖並實現一個將在該UIButton上發生事件時執行的方法。
代碼的方法是什麼樣子,
if(tableView.editing)
[tableView setEditing:NO animated:YES]
else
[tableView setEditing:YES animated:YES]
閱讀UITableView Documentation獲取更多信息。
如果我添加的額外代碼無法幫助您發佈您正在使用的內容?謝謝 – 2011-05-10 01:53:23