Недавно я начал программирование для iPhone, и я создаю приложение, которое подключается к базе данных и получает набор имен строк и отображает их. Когда выбрано, изменение цвета фона строк, т.е. Вы можете сделать несколько выборов, и все они будут разных цветов. Поэтому я возвращаю XML с сервера без проблем, и я создал UITableView
для отображения ячеек. Однако я не знаю, как добавить ячейки в таблицу. Я посмотрел на insertRowsAtIndexPaths
, но я не уверен, как его использовать? Насколько я понимаю, insertRowsAtIndexPaths
принимает два параметра:
NSArray, содержащий строку, в которой ячейка должна находиться и в каком разделе. Проблема заключается в том, что мое приложение будет иметь динамическое количество строк. Как я могу создать NSArray, если не знаю, сколько строк у меня будет? Можно ли использовать NSMutableArray?
Второй параметр, который требуется, - это анимация - это довольно просто.
Другая проблема, с которой я сталкиваюсь, - это где я действительно создаю ячейки? Как передать ячейки в таблицу?
Я пробовал читать документацию, но это просто не очень понятно! Вот код, который у меня есть в данный момент в методе 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];