• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UITableViewCell 自定义添加到收藏夹按钮问题

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:58:15 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在我的表格 View 中,我在每个单元格中放置了一个自定义的“添加到收藏夹”按钮,以使用户能够将确切的单元格内容复制到第二个表格 View Controller 。当您点击“添加到收藏夹”按钮时,会出现一个警报 View ,询问您是否要复制单元格并将其粘贴到第二个 View Controller 。现在有两件事。 1-如果从警报 View 中选择“确定”以指示单元格被复制并粘贴到第二个表格 View ,是否有办法从该单元格中永久删除“添加到收藏夹”按钮? - 因此用户将无法一遍又一遍地添加单元格内容。 2- 这是一个更大的问题:我如何通过单击“添加到收藏夹”将单元格内容复制并粘贴到 secondtableview Controller ?

这是我的细胞重新配置的方式:

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    NSString* letter = [letters objectAtIndex:indexPath.section];
    NSArray* arrayForLetter = (NSArray*)[filteredTableData objectForKey:letter];
    Songs* songs = (Songs*)[arrayForLetter objectAtIndex:indexPath.row];

    cell.textLabel.text = songs.name;
    cell.detailTextLabel.text = songs.description;

    CGSize itemSize = CGSizeMake(50, 50);
    UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIButton *addtoFavsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    addtoFavsButton.frame = CGRectMake(200.0f, 5.0f, 105.0f, 70.0f);
    [addtoFavsButton setImage:[UIImage imageNamed"fav.png"] forState:UIControlStateNormal];
    [addtoFavsButton setTintColor:[UIColor whiteColor]];
    [cell addSubview:addtoFavsButton];
    [addtoFavsButton addTarget:self
                        actionselector(addtoFavs
              forControlEvents:UIControlEventTouchUpInside];
    return cell;
    }
- (IBAction)addtoFavsid)sender
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle"Dikkaaaat!"
                                                   message"Şarkıyı Favori Akorlarınıza Alıyorsunuz..."
                                                  delegate:nil
                                         cancelButtonTitle"Cancel"
                                         otherButtonTitles"OK", nil];
            [alert show];
    }

enter image description here enter image description here



Best Answer-推荐答案


考虑到数据仅在应用程序中,并且考虑到您使用的是常规表格单元格,而不是自定义单元格,我的建议是:

  • 在您的应用中创建一个名为“myFavorites”或其他名称的新简单数组,该数组将仅包含数值列表。

  • 当用户确认添加到收藏夹时,从歌曲数组中获取当前部分和行索引,并存储在这个新数组中。

  • 在您的第一个 View Controller 中,向“cellForRowAtIndexPath”添加一个简单的检查,以查看该行的歌曲是否存在于新数组中。

类似:

 if([[myFavorites objectAtIndex:indexPath.section] containsObject:[NSString stringWithFormat"%ld", (long)indexPath.row]]){
     // Don't make button because is already a favorite.
 }else{
     // Make button because not yet a favorite.
 }

您的第二个表格 View 将几乎相同,除了“cellForRowAtIndexPath”顶部附近,请进行类似检查:

if([[myFavorites objectAtIndex:indexPath.section] containsObject:[NSString stringWithFormat"%ld", (long)indexPath.row]]){
     // Is a favorite, so put regular cell stuff here to have the cell show
     ....

 }else{
     // Not a favorite, don't generate cell.
     cell.visible = NO;
     return cell;
 }

您还可以做其他事情,但这需要将您的设置从常规单元更改为具有新类和属性等等的自定义单元,因此实现起来有点复杂,但实际上仍相当于相同的输出/处理。

关于ios - UITableViewCell 自定义添加到收藏夹按钮问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30759425/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap