我希望我的單元格不僅在編輯時有刪除按鈕,還有其他人。那麼,這是一個實際的方法呢?我已經嘗試爲editingAccessoryView
屬性製作自己的UIView
對象,但問題在於該視圖僅在直接編輯時出現(即單擊導航欄中的「編輯」按鈕),並完全忽略單元格上的滑動。另外tableView:editActionsForRowAtIndexPath:
方法被嘗試,但沒有奏效,至少我沒有設法弄清楚如何得到我想要的功能。先謝謝你!UITableViewCell自定義編輯內容
回答
你試試這個代碼
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *button = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@"Action to perform with Button 1");
}];
button.backgroundColor = [UIColor greenColor]; //arbitrary color
UITableViewRowAction *button2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Button 2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
NSLog(@"Action to perform with Button2!");
}];
button2.backgroundColor = [UIColor blueColor]; //arbitrary color
return @[button, button2]; //array with all the buttons you want. 1,2,3, etc...
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// you need to implement this method too or nothing will work:
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES; //tableview must be editable or nothing will work...
}
您CanTry也驗證碼
.H File
{
NSMutableArray *dataArray;
}
@property (weak, nonatomic) IBOutlet UITableView *atableView;
@property(nonatomic,strong) NSMutableArray *dataArray;
**`.M file`**
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataArray = [[NSMutableArray alloc] initWithObjects:@"Saurabh Sharma",@"Deepesh Jain",@"Ashish Sharma",@"Chandan",@"kanhaiya",@"Suchitra Bohra",@"Neha",@"Ghanshyam",nil];
self.title = @"Move Rows";
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(editButton:)];
[self.navigationItem setRightBarButtonItem:editButton];
// Do any additional setup after loading the view from its nib.
}
-(void)editButton:(UIBarButtonItem *)button
{
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[atableView setEditing:NO animated:NO];
[atableView reloadData];
[self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[atableView setEditing:YES animated:YES];
[atableView reloadData];
[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *[email protected]"cellIdentifair";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifair];
if(cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifair];
}
cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma delete row in table view
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 10; // This is the minimum inter item spacing, can be more
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return TRUE;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
[dataArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates];
NSLog(@"Methods called when row is deleted");
}
#pragma Move Table View Delegte
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *item = [self.dataArray objectAtIndex:fromIndexPath.row];
[self.dataArray removeObject:item];
[self.dataArray insertObject:item atIndex:toIndexPath.row];
}
歡迎和Thankuuu – 2014-10-10 13:12:25
歡迎和Thankuuu – 2014-10-10 13:15:52
上面的代碼片段很混亂(你正在混合集合視圖委託和表視圖委託,使用舊的ObjC語法),並沒有解決原始問題。 – 2014-11-27 10:46:30
作品在斯威夫特也是如此。
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
var completionButton = UITableViewRowAction(style: .Normal,
title: "Complete",
handler: {(action, index) in
/* Completion Code */
println("Complete")})
completionButton.backgroundColor = .lightGrayColor()
return [completionButton]
}
也適用於iOS 11,你應該使用
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
- 右輕掃
和
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
- 爲向左掃
對於前:
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
UIContextualAction* action = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"Title" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
// Do something
completionHandler(YES);
}
UISwipeActionsConfiguration * actionsConfiguration = [UISwipeActionsConfiguration configurationWithActions:@[action]];
return actionsConfiguration;
}
- 1. 自定義UITableViewCell編輯模式圖標
- 2. 自定義的UITableViewCell在編輯模式
- 3. UITableViewCell自定義編輯視圖
- 4. 自定義UITableViewCell編輯樣式
- 5. 自定義的UITableViewCell編輯模式
- 6. 自定義UITableViewCell編輯動畫
- 7. IOS5 - 自定義UITableViewCell不顯示內容
- 8. 自定義和動態內容UITableViewCell
- 9. 自定義UITableViewCell的內容未顯示
- 10. UITableViewCell,隱藏自定義內容
- 11. 自定義UITableViewCell的設置內容
- 12. UITableViewCell單擊編輯單元格內容
- 13. UiTableView內部自定義UITableViewCell
- 14. 敲除內容可編輯自定義綁定
- 15. 使用自定義表單添加/編輯自定義內容類型
- 16. 如何編輯位於自定義ASPX中的內容編輯器Web部件?
- 17. X-編輯自定義彈出了懸停內容輸入
- 18. 自定義Sitecore的內容編輯器語言選擇
- 19. Wordpress自定義內容區域與WP編輯器?
- 20. 自定義wordpress編輯器的輸出內容?
- 21. 添加自定義風格,內容編輯
- 22. MVC 5自定義HtmlHelper輕量級內容編輯
- 23. SharePoint內容編輯器CSS自定義緩存
- 24. Sitecore爲內容編輯器創建自定義控件
- 25. 自定義UITableViewCell
- 26. 自定義UITableViewCell
- 27. UITableViewCell自定義
- 28. 自定義UITableViewCell
- 29. UITableViewCell自定義
- 30. 編輯自定義UITableViewCell時不會出現插入/刪除編輯控件
非常感謝!它工作正常! – Majotron 2014-10-10 12:41:45
歡迎和plz右標記點擊這個問題 – 2014-10-10 12:44:14
順便說一句,你可以請說什麼是正確的方式來顯示這些行動只有當用戶滑過單元格,但沒有點擊編輯按鈕? – Majotron 2014-10-10 12:50:46