2013-03-31 33 views
1

我正在創建一個基於TableView的應用程序。 tableView正在加載XML提要的外部最後12項內容。這一切都很完美。從UITableView保存項目

所以現在我想創建一個額外的「保存喜歡的項目功能」。有2種方式來實現這一目標:

1.定製AccessoryButton

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

2(定製)編輯的tableview

if ([self.tableView isEditing]) 

我的問題是:你更喜歡哪一個選項你能舉出一個如何實現這個目標的例子嗎?

任何認真的答覆將不勝感激。

謝謝你的回答。由於馬特我用下面的代碼固定它:

 NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row]; 
    BOOL checked = [[item objectForKey:@"checked"] boolValue]; 
    //cell.backgroundColor = [UIColor clearColor]; 
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    UIImage *image = (checked) ? [UIImage imageNamed:@"first.png"] : [UIImage imageNamed:@"second.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; 

正如你可以通過IM使用dataAray現在看到。我也使用存儲「檢查布隆」的plist。 (根據plist中)

  • 當UITableView的滾動型移動對號radomly改變

    1. 對號不放在正確的:這不能正常工作,因爲。

    所以我想創建一個存儲所選項目的Id的數組。然後遍歷數組以查看數組中是否存在ID。如果是:Star如果不是:graystar。

    您認爲這是一個很好的解決方案嗎?

  • 回答

    0

    我非常喜歡配件按鈕的方法。由於編輯模式通常用於刪除或重寫項目,用戶通常不會在那裏看到或期望它執行任何其他功能。

    話雖如此,我不會建議使用附件按鈕,而不更改其默認圖像。由於配件按鈕的默認圖像通常意味着顯示有關當前項目的更多詳細信息,因此如果不更改爲更具描述性的圖像,可能會導致混淆。此外,如果當前項目被標記爲最愛(例如,如果它不是最喜歡的灰星,並且如果它是金星),則最佳地圖像應該不同。代碼應該是非常直接的:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    
        cell = UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"]; 
        if(!cell) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Identifier"] 
    
        UIImage *accessoryImage; 
        ItemClass *item = [self.items objectAtIndex:indexPath.row]; 
        // you might want to cache the images instead of creating them for each cell 
        if(item.favorite) 
        accessoryImage = [UIImage imageNamed:@"goldenStar.png"]; 
        else 
        accessoryImage = [UIImage imageNamed:@"greyStar.png"]; 
        cell.accessoryView = [[UIImageView alloc] initWithImage:accessoryImage]; 
    } 
    
    -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { 
        ItemClass *item = [self.items objectAtIndex:indexPath.row]; 
        item.favorite = !item.favorite; 
    
        // update the cell with the new image and any other data you need modified 
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
    } 
    

    祝你好運!

    +0

    你是什麼意思用:itemClass? – Foo

    +0

    這就是你爲你的物品創建的任何課程。 – Matt