У меня есть tableView с 2 разделами. Я могу поместить 3 строки в настоящее время в первую очередь 2 из первой секции и 1 строку второй секции. Всякий раз, когда я получаю текст с сервера, я хочу, чтобы новая строка была вставлена на первое место во втором разделе. Как это:uitableview insertRowAtIndexPath удваивает строки
--------- ---------
| 0-0 | | 0-0 |
--------- ---------
| 0-1 |------>| 0-1 |
--------- ---------
| 1-0 | |new 1-0|
--------- ---------
| 1-1 |
---------
Я попытался с помощью insertRowsAtIndexPath и reloadSections способа представления таблицы, но оба из них действительно добавляет новую строку в начале раздела, но также дублируется строки первой секции (2 в данном случае). Как это:
--------- ---------
| 0-0 | | 0-0 |
--------- ---------
| 0-1 |------>| 0-1 |
--------- ---------
| 1-0 | | 0-0 |
--------- ---------
| 0-1 |
---------
|new 1-0|
---------
| 1-1 |
---------
добавить новую строку при получении уведомления через NSNotificationCenter здесь фрагмент кода:
-(void)receiveNotification:(NSString *)notification
{
[myArray1 insertObject:notification atIndex:0];
[self.tblView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
// Alternate Code
// [self.tblView beginUpdates];
// [self.tblView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
// [self.tblView endUpdates];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return [myArray0 count];
else
return [myArray1 count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
static NSString *cellId = @"cell0";
Section0 *cell0 = [tableView dequeueReusableCellWithIdentifier:cellId];
//Manage first section cell
return cell0;
}
else
{
static NSString *cellId = @"cell1";
Section0 *cell1 = [tableView dequeueReusableCellWithIdentifier:cellId];
// Manage second section cell
return cell1;
}
}
Не могли бы вы поделиться кодом? –
Это не должно произойти, поэтому вы явно используете его неправильно, поэтому поделитесь своим кодом. Какие функции вы называете и где? –
@JavierFloresFont Я не могу использовать код. Я попробую сделать еще один демо-код для репликации проблемы, пока вы не сможете поделиться с ним демо-кодом. – meteors