Добавить последнюю ячейку в UItableview - PullRequest
3 голосов
/ 20 мая 2011

У меня есть UITableView , источником данных которого является NSMutableArray . Массив состоит из набора объектов. Все ячейки отображаются в правильном порядке.

Теперь я хочу знать, как всегда отображать последнюю ячейку с одним только текстом, которого нет в массиве источника данных.

Надеюсь, я достаточно ясно:)

РЕДАКТИРОВАТЬ: ----------

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     // Return the number of rows in the section.
     //+1 to add the last extra row
     return [appDelegate.list count]+1;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

NSUInteger index=[indexPath row];

 if(index ==([appDelegate.list count]+1)) {
    cell.textLabel.text = [NSString stringWithFormat:@"extra cell"];    
    }else{
    Item *i = (Item *) [appDelegate.list objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ (%d %@)",i.iName, i.iQty,i.iUnit];
    }
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

но я получаю NSMutableArray за пределами исключения.

Что может быть не так?

Ответы [ 3 ]

5 голосов
/ 20 мая 2011
    - (NSInteger)tableView:(UITableView *)tableView
     numberOfRowsInSection:(NSInteger)section
    {
       return [your_array count] + 1;
    }

- (UITableViewCell *)tableView:(UITableView *)tableView
 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
   UITableViewCell *cell = [tableView
   dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
   if (cell == nil) {
      cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
      reuseIdentifier:SimpleTableIdentifier] autorelease];
   }

   NSUInteger row = [indexPath row];
   if (row == [your_array count])
   {
      cell.textLabel.text = [NSString stringWithFormat:@"Some text"];
   }
   else
   {
      cell.textLabel.text = your array object text;
   }
   return cell;
}
1 голос
/ 20 мая 2011

Ваша проблема - значение, которое вы проверяете по индексу. list имеет количество объектов с индексом 0 для подсчета - 1. Таким образом, вы должны проверить count, а не count + 1. Как и сейчас, запрос для count -ой строки входит в раздел else. В массиве list нет объекта на count. Итак, вы получаете ошибку. Это модификация.

if(index == [appDelegate.list count] ) {
    cell.textLabel.text = [NSString stringWithFormat:@"extra cell"];    
}else{
    Item *i = (Item *) [appDelegate.list objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ (%d %@)",i.iName, i.iQty,i.iUnit];
}
1 голос
/ 20 мая 2011

in numberOfRowInSection return number of rows = your array count +1 затем в Cell для cellForRowAtIndexPath проверьте indexPath.row, если он равен вашему массиву count +1, затем создайте ячейку, которую вы хотите добавить наконец.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...