2013-06-22 47 views
0

我試圖添加一個「收藏夾」明星到現有的tableview列表。星星是一個按鈕,存在於每個單元格內。當星星被按下時,我想將它包含的單元格添加到新的tableview中,從而創建一個收藏夾列表。嘗試添加單元格從現有的tableview到一個新的tableview時按下按鈕(添加到收藏夾按鈕)

例如,如果按下按鈕,將單元格添加到favoritesTableView。

有沒有關於這個(我可以看到)的在線文檔,但這是我迄今爲止。

注:SearchResult所=收藏項目*

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]; 
} 

回答

0

「我知道我會得到一些管理的評價是,這是不是一個答案」,但一個更簡單的方法就是在用戶決定要查看收藏夾後,重新載入您的表格視圖和收藏夾信息數組。除此之外,你有什麼問題,我會進一步重新編輯我的答案來幫助你,你根本沒有得到任何結果?下面

將答案:

看起來像你的情況這符合您的需求

- (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]]; 

我不知道,如果你改變原來的表和收藏夾表,但如果他們實際上是相同的,那麼你可以簡單的重啓之間的意見在按下按鈕的表格中,除非您不想根據導航設置的方式切換視圖的動畫。希望這有助於。

+0

嗨!我只是想弄清楚如何創建收藏夾陣列。例如,一旦用戶看到完整的咖啡館列表,一旦用戶點擊「星級」按鈕來收藏,我可以使用什麼樣的代碼來告訴應用程序保存此收藏夾,然後將其加載到收藏夾表格中?謝謝! –

+0

根據您的需求更新了答案。 – rezand

+0

驚人的參考。好的,看看我上面更新的代碼(.m文件)。出於某種原因,在執行此操作後,模擬器在我的else語句下爲此行引發了SIGBRT錯誤:cell.titleLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@「Title」]; –

相關問題