2009-12-06 25 views
2

Three20是一個偉大的庫。它使表格的工作變得非常簡單。但我注意到的一個缺陷是刪除了行,並帶有動畫。我花了很多時間試圖做到這一點,這是我在做什麼:如何刪除TTTableViewController中的行(Three20)

// get current index patch 
UITableView* table = [super tableView]; 
NSIndexPath *indexPath = [table indexPathForSelectedRow]; 

//delete row in data source 
TTListDataSource * source = (TTListDataSource *)self.dataSource; 
[source.items removeObjectAtIndex:indexPath.row]; 

// delete row from the table, with animation 
[table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

此代碼後運行出現錯誤:[NSCFArray objectAtIndex:]:指數(0)超出範圍(0)

我正在使用從TTTableViewController繼承的類。我沒有實現DataSourceDelegate或TableViewDelegate,因爲我儘可能地使用了Three20。所以,這是我如何創建數據源:

for(NSDictionary *album in (NSArray *)albums){ 

TTTableRightImageItem *item = [TTTableRightImageItem itemWithText:@"name" 
      imageURL:@"image url" 
      defaultImage:nil 
      imageStyle:TTSTYLE(rounded) 
      URL:@"url of selector where I actually catch the tap on a row"]; 
[tableItems addObject:item]; 
} 
self.dataSource = [TTListDataSource dataSourceWithItems:tableItems]; 

我看了這篇文章(article),但它並不能幫助:(

所以,我知道,這是從這個網站的大師很簡單。你能不能給我如何做到這一點

感謝

回答

1

我發現的第一件事情是,你刪除從表中的一行時,該表是不是在編輯模式先進的樣品。

+0

好的。這是重要的評論。我在行「NSIndexPath * indexPath = [table indexPathForSelectedRow];」之後加了一行「table.editing = YES;」。結果是一樣的:( – Dmitry 2009-12-06 19:49:48

+1

類TTSectionedDataSource不足以在可編輯表中使用,因爲它沒有實現tableView:commitEditingStyle:forRowAtIndexPath。Subclass TTSectionedDataSource並實現ableView:commitEditingStyle:forRowAtIndexPath。在這個方法中刪除該項目,然後調用didDeleteObject。這個方法會通知你的控制器並執行動畫。 – lyonanderson 2009-12-06 20:30:22

+0

其實我只需要用動畫程序去除行,就像這裏:http://stackoverflow.com/questions/915095/how-to-delete-a我注意到在TTSectionedDataSource中有「 - (void)removeItemAtIndexPath:(NSIndexPath *)indexPath」方法,但它拋出一個錯誤!!也許這是Three20中的一個錯誤 – Dmitry 2009-12-06 21:48:22

4

我還在model:didDeleteObject:atIndexPath:中得到了「EXC_BAD_ACCESS」,但我解決了這個問題。

要刪除一個TTTableViewController一排,你需要在你的DataSource實現這樣的實施tableView:commitEditingStyle:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
     forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     id object = [[self.items objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 

     [self.model didDeleteObject:object atIndexPath:indexPath]; 
    } 
}

您還需要實現tableView:willRemoveObject:atIndexPath:在您的數據源:

- (NSIndexPath*) tableView:(UITableView *)tableView willRemoveObject:(id)object atIndexPath:(NSIndexPath *)indexPath { 
    // delete the object 
    [object deleteFromDB]; 

    // removeItemAtIndexPath returns YES if the section is removed 
    if ([self removeItemAtIndexPath:indexPath andSectionIfEmpty:YES]) { 
     // remove an index path only to that section 
     return [NSIndexPath indexPathWithIndex:indexPath.section]; 
    } else { 
     return indexPath; 
    } 
}

的訣竅是更改NSIndexPath您只返回包含它的部分變空。然後,TTTableViewController中的代碼將刪除該部分而不是單元格。

HTH

+0

我想知道你爲什麼調用'[self.model didDeleteObject:對象atIndexPath:indexPath];在數據源的'tableView:willRemoveObject:atIndexPath:'方法中。你不應該調用'[self.model removeSomeObject:... atIndexPath:indexPath]'並讓這個對象調用'[self didDeleteObject:object atIndexPath:indexPath]'而不是? – 2011-08-25 22:03:05

相關問題