这是我的代码, 我想用减号删除红色按钮并使其在向左滑动时显示删除按钮,现在滑动不起作用,我检查了 UITableViewCellEditingStyleDelete 它默认显示红色按钮,但我在网上看到的视频使用了这种方法,但是只需用减号轻扫红色按钮即可工作
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *musicPlaylistCellId = @"feedCell";
UITableViewCell *cell = nil;
if(tableView == self.historyTableView){
FeedCell *musicPlaylistCell = [self.historyTableView dequeueReusableCellWithIdentifier:musicPlaylistCellId];
[musicPlaylistCell setContentWithPlaylist:[self.historyList objectAtIndex:indexPath.row]];
cell = musicPlaylistCell;
}
else if (tableView == self.myMusicTableView) {
UserFeedCell *musicPlaylistCell = [self.myMusicTableView dequeueReusableCellWithIdentifier:musicPlaylistCellId];
musicPlaylistCell.isMyProfile = isMyProfile;
// [musicPlaylistCell addActionButtons:[self leftButtons] withButtonWidth:kJAButtonWidth withButtonPosition:JAButtonLocationRight];
musicPlaylistCell.delegate = self;
BOOL isLibrary = (self.btnLibraryTop.selected || self.btnLibrary.selected);
musicPlaylistCell.isRecent = !isLibrary;
Playlist *obj;
if (isLibrary) {
obj = (Playlist *)[self.myMusicList objectAtIndex:indexPath.row];
musicPlaylistCell.btnPlaylistOwner.hidden = YES;
musicPlaylistCell.vwActions.hidden = NO;
}
else
{
obj = (Playlist *)[self.aryRecentPlaylists objectAtIndex:indexPath.row];
musicPlaylistCell.btnPlaylistOwner.hidden = NO;
musicPlaylistCell.vwActions.hidden = YES;
}
if (obj.originalOwner != nil) {
musicPlaylistCell.isMyProfile = NO;
}
[musicPlaylistCell setContentWithPlaylist:(isLibrary?[self.myMusicList objectAtIndex:indexPath.row]:[self.aryRecentPlaylists objectAtIndex:indexPath.row]) withTrackNumbers:NO];
if (isLibrary) {
if (indexPath.row == [self.myMusicList count] - 1)
{
[self launchReload];
}
}
else
{
if (indexPath.row == [self.aryRecentPlaylists count] - 1)
{
[self launchReload];
}
}
// if (indexPath.row == rowReload){
// [musicPlaylistCell revealButtonsWithTopViewWithOffset:-60 swipeDirection:JASwipeDirectionLeft];
// }
cell = musicPlaylistCell;
}
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return true;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.myMusicList removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}[enter image description here](https://i.stack.imgur.com/elJW2.jpg)
听起来您想自定义 Xcode 中 UITableView 或 UICollectionView 中删除按钮的行为。为此,您需要实现滑动删除功能,因为默认行为包括用于删除的红色减号按钮。
1。 实现滑动删除:
确保视图控制器中有 UITableView 或 UICollectionView。您需要为这些视图中的单元格实现滑动删除功能。
2。 覆盖编辑样式:
在 UITableViewDelegate 或 UICollectionViewDelegate 中,您可以重写 EditingStyleForRowAt 或 editActionsForItemAt 方法,具体取决于您使用的是表视图还是集合视图。
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Handle the deletion here
}
}
对于 UICollectionView:
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let deleteAction = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill"), identifier: nil) { [weak self] _ in
// Handle the deletion here
}
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
UIMenu(title: "", children: [deleteAction])
}
}
3. 自定义删除按钮外观(可选):
如果您想自定义删除按钮的外观,可以在创建 UIAction 实例时进行。您可以设置不同的标题、图像或其他属性,使其看起来像您想要的那样。
4。删除红色减号按钮:
通过实现您自己的滑动删除行为,默认情况下不应再出现红色减号按钮。相反,当您在单元格上向左滑动时,您会看到自定义删除按钮。
5。确保编辑模式已启用:
当您想要允许滑动删除时,请仔细检查您的表视图或集合视图是否处于编辑模式。您可以通过在 UITableView 或 UICollectionView 实例上将 isEditing 属性设置为 true 来启用编辑模式。
tableView.isEditing = true // Enable editing mode for a UITableView
collectionView.allowsSelectionDuringEditing = true // Enable editing mode for a UICollectionView
请记住将代码示例中的注释替换为您实际的删除逻辑。实施这些步骤后,向左滑动应触发自定义删除按钮,并且红色减号按钮不应再出现。