【问题标题】:Trying to add cell from existing tableview to a new tableview when a button is pressed (Add to favorites button)按下按钮时尝试将现有表格视图中的单元格添加到新表格视图(添加到收藏夹按钮)
【发布时间】:2013-06-22 22:33:46
【问题描述】:

我正在尝试将“收藏夹”星添加到现有的 tableview 列表中。星星是一个按钮,存在于每个单元格内部。当按下星号时,我想将它包含的单元格添加到一个新的 tableview 中,从而创建一个收藏夹列表。

例如如果按下按钮,则将单元格添加到收藏夹表视图。

几乎没有在线文档(我可以看到),但这是我目前所拥有的。

注意:searchResults = 收藏项*

TableView.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *strainTableIdentifier = @"StrainTableCell";

    StrainTableCell *cell = (StrainTableCell *)[tableView dequeueReusableCellWithIdentifier:strainTableIdentifier];
    if (cell == nil) 


        cell = [[[StrainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strainTableIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;



        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StrainTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    if (tableView == self.searchDisplayController.searchResultsTableView) {
       NSLog(@"Using the search results");

        cell.titleLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"];
        cell.descriptionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Description"];
        cell.ratingLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Rating"];
        cell.ailmentLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Ailment"];
        cell.actionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Action"];
        cell.ingestLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Ingestion"];

        NSLog(@"%@", searchResults);





    } else {
        NSLog(@"Using the FULL LIST!!");
        cell.titleLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
         cell.descriptionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Description"];
         cell.ratingLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Rating"];
        cell.ailmentLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Ailment"];
        cell.actionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Action"];
        cell.ingestLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Ingestion"];

    }


    NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"text"];

    [item setObject:cell forKey:@"StrainTableCell"];
    BOOL checked = [[item objectForKey:@"checked"] boolValue];
    UIImage *image = (checked) ? [UIImage   imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;

return cell;


}


- (void)checkButtonTapped:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
    if (indexPath != nil)
    {
        [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
    }
}



- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
    BOOL checked = [[item objectForKey:@"checked"] boolValue];
    [item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
    UITableViewCell *cell = [item objectForKey:@"StrainTableCell"];
    UIButton *button = (UIButton *)cell.accessoryView;
    UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
}

【问题讨论】:

    标签: objective-c view uitableview xcode4.5


    【解决方案1】:

    “我知道我会得到一些管理员的评论,这不是一个答案”,但更简单的方法是在用户决定他们想要查看收藏夹后重新加载带有收藏夹信息数组的表格视图.除此之外,您对此有什么问题,我将进一步重新编辑我的答案以帮助您,您根本没有得到任何结果吗?

    在下面添加答案:

    看起来适合您的情况,这符合您的需求

    - (void)checkButtonTapped:(id)sender event:(id)event
    {
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
    if (indexPath != nil)
    {
    [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
    }
    

    http://www.edumobile.org/iphone/iphone-programming-tutorials/impliment-a-custom-accessory-view-for-your-uitableview-in-iphone/上的代码是这样的

    代码以调用结束

    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
    

    尽管您可以替换它或在该方法中添加您需要的代码。您需要的代码是创建一个 NSMutablearray 我假设您知道如果您不熟悉它,请确保在添加一些内容后通过运行 nslog 对其进行测试。一旦你有你的 NSMutablearray 收藏夹,你会添加类似...

    [myFavoritesArray addobject:[myOriginalArray objectAtIndex:indexPath]];
    

    我不确定您是否正在更改原始表格和收藏夹表格之间的视图,但如果它们实际上相同,那么您可以通过按下按钮重新加载表格,除非您不想要切换视图的动画当然取决于导航的设置方式。希望这会有所帮助。

    【讨论】:

    • 嗨!我只是想弄清楚如何创建收藏夹数组。例如,一旦用户看到完整的咖啡馆列表,一旦用户点击“星号”按钮进行收藏,我可以使用什么样的代码告诉应用程序保存这个收藏,然后将其加载到收藏表?谢谢!
    • 惊人的参考。好的,看看我上面更新的代码(.m 文件)。出于某种原因,在执行此操作后,模拟器在我的 else 语句下向我抛出了 SIGBRT 错误:cell.titleLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
    • 哦...这是您拥有的关键部分的对象...这是一个 nsdictionary 类型函数而不是 nsmutablearray 但我知道您在这里要做什么...我只需要一秒钟再看代码
    • 非常感谢您的帮助。这一直让我发疯。待命... :)
    • 我迷路了,试图在堆栈溢出 cmets 中交谈让我感到很痛苦...你能把你的两个 tableview 课程发邮件给我吗?IPhoneHungry@yahoo.com
    猜你喜欢
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多