2013-11-25 2 views
0

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
mSectionArray = [NSArray arrayWithObjects:@"abc", @"def",@"ghi", nil] ; 
return [mSectionArray count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 


if(section == 0) { 
    mContentArray = [NSArray arrayWithObjects:@"hello",@"hello1", nil]; 


} 
if(section == 1) { 
    mContentArray = [NSArray arrayWithObjects:@"one", @"two",@"three",@"four",@"five", nil]; 
    NSLog(@"section 1 %@",mContentArray); 


}if (section == 2){ 
    mContentArray = [NSArray arrayWithObjects:@"abc", nil]; 
} 
return [mContentArray count]; 

} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

NSString *sectionHeader = nil; 

if(section == 0) { 
    sectionHeader = @"abc"; 
} 
if(section == 1) { 
    sectionHeader = @"def"; 
} 
if (section == 2) { 
    sectionHeader = @"ghi"; 
} 

return sectionHeader; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
UITableViewCell *cell = nil; 
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

if(!cell){ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"]; 
} 
cell.textLabel.text = [mContentArray objectAtIndex:indexPath.row]; 
cell.textLabel.textColor = [UIColor darkGrayColor]; 
return cell; 
} 

Он показывает «один» «два» «три» и т. Д. Во всех разделах, но мне нужны разные значения для разных разделов. помогите мне в решении проблемы.

+2

примечания стороны - пожалуйста, узнать о 'if-else' заявлении. – rmaddy

+0

@Newbee проверить мой обновленный ответ. Он разрешил сбой –

ответ

1

Ваш массив должен быть такой:

intialize in viewDidLoad 

    mContentArray = [[NSMutableArray alloc]init]; 

    NSArray * araray1 = [[NSArray alloc] initWithObjects:@"hello",@"hello1", nil]; 
    NSArray * araray2 = [[NSArray alloc] initWithObjects:@"one", @"two",@"three",@"four",@"five", nil]; 

    NSArray * araray3 = [[NSArray alloc] initWithObjects:@"abc", nil]; 

    [mContentArray addObject:araray1]; 
    [mContentArray addObject:araray2]; 
    [mContentArray addObject:araray3]; 



    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

    { 
     return [mContentArray count]; 
    } 

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {   

     return [[mContentArray objectAtIndex:section]count]; 

    } 

при получении

cell.textLabel.text = [[mContentArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]; 
+0

Использовать современный синтаксис - cell.textLabel.text = mContentArray [indexPath.section] [indexPath.row]; '. – rmaddy

+0

Есть ли какое-то преимущество использования этого современного синтаксиса? Если да, объясните, пожалуйста, –

+0

Да, его гораздо легче читать и печатать. – rmaddy

Смежные вопросы