我剛剛開始爲iPhone編程,我正在創建一個連接到數據庫並獲取一組行名稱並顯示它們的應用程序。選中後,行的背景顏色會發生變化,即您可以進行多項選擇,並且它們都將是不同的顏色。所以我從服務器上取回XML沒有問題,我創建了一個UITableView
來顯示單元格。但是,我不知道如何將單元格添加到表格中。我看過insertRowsAtIndexPaths
,但我不確定如何使用它?據我所知,insertRowsAtIndexPaths
需要兩個參數:以編程方式向UITableView添加單元格
一個NSArray,它包含單元格應該在哪一行以及哪一部分。這個問題是我的應用程序將有一個動態的行數。如果我不知道有多少行,我將如何去創建NSArray?我可以使用NSMutableArray嗎?
它需要的第二個參數是一個動畫 - 這非常簡單。
我遇到的另一個問題是我在哪裏創建單元格?你如何將單元格傳遞給tableview?
我試過閱讀文檔,但它似乎不太清楚!這是在視圖控制器的loadView方法中的那一刻我的代碼有:
//Before this I get the XML from the server so I am ready to populate
//cells and add them to the table view
NSArray *cells = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0],
[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0],
[NSIndexPath indexPathForRow:4 inSection:0],
[NSIndexPath indexPathForRow:5 inSection:0],
[NSIndexPath indexPathForRow:6 inSection:0],
[NSIndexPath indexPathForRow:7 inSection:0],
[NSIndexPath indexPathForRow:8 inSection:0],
[NSIndexPath indexPathForRow:9 inSection:0],
[NSIndexPath indexPathForRow:10 inSection:0],
[NSIndexPath indexPathForRow:11 inSection:0],
[NSIndexPath indexPathForRow:12 inSection:0],
nil];
[eventTypesTable beginUpdates];
[eventTypesTable insertRowsAtIndexPaths:cells withRowAnimation:UITableViewRowAnimationNone];
[eventTypesTable endUpdates];
謝謝,我知道我完全搞不清怎麼奏效。我已經設法正確輸出這些單元格,但是我遇到了問題。我從數據庫檢索12行,但屏幕只適合7.一旦屏幕已滿,並在模擬器,如果我試着向下滾動我得到一個錯誤'NSString * sEventType = [[eventTypes valueForKeyPath:@「name .text「] objectAtIndex:indexPath.row];'cellForRowAtIndexPath'方法內部。有什麼我失蹤?像我必須添加一個滾動控制器或什麼?再一次感謝您的及時和有益的迴應! – KerrM 2012-02-24 12:37:44
鏈接已損壞。這就是爲什麼你至少應該發佈一些示例代碼而不是依靠外部鏈接。 – 2014-02-28 19:16:38
鏈接已損壞,但您可以在此處找到此網址的內容:http://web.archive.org/web/20150928131750/http://www.iosdevnotes.com/2011/10/uitableview-tutorial/ – Upsilon42 2016-05-04 13:35:49