OGeek|极客世界-中国程序员成长平台

标题: ios - UITableViewCell 自定义添加到收藏夹按钮问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:58
标题: ios - UITableViewCell 自定义添加到收藏夹按钮问题

在我的表格 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-推荐答案


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

类似:

 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4