Ashish Gabani
Ashish Gabani

Reputation: 443

Multiple section with one array in tableview iOS

i am newbie in iOS development. my array contain 50 values i want to show this 50 values in one by one in 50 section then i write a code for that like as

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
return self.dataArray.count;
 }
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}

then i got 50 section but all section contain same array value i want to show that each section contain one by one data.Please give me any solution if it was possible

My TablwviewCell:Rowatindexpath method

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=@"Cell";
CustumCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (cell == nil)
{
    NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"CustumCell" owner:self options:nil];
    cell=[nib objectAtIndex:0];
}

NSDictionary *dict = [self.imagesa objectAtIndex:indexPath.item];
NSString *img2=[dict valueForKey:@"post_image"];

[cell.photoimage sd_setImageWithURL:[NSURL URLWithString:[img2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"Hisoka.jpg"] options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"downloaded");

    });
}];
NSString *name=[dict valueForKey:@"post_title"];
cell.namelabel.text=name;
NSString *des=[dict valueForKey:@"post_content"];
cell.deslabel.text=des;


NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
 NSString *date=[dict valueForKey:@"post_date"];
NSDate * dateNotFormatted = [dateFormatter dateFromString:date];
[dateFormatter setDateFormat:@"d-MMM-YYYY"];
NSString * dateFormatted = [dateFormatter stringFromDate:dateNotFormatted];
NSLog(@"Date %@",dateFormatted);
cell.datelabel.text=dateFormatted;
return cell;
}

Upvotes: 0

Views: 741

Answers (3)

iYoung
iYoung

Reputation: 3622

use indexpath.sectioninstead ofindexpath.rowin cellForRowAtIndexPath.

Upvotes: 0

pbasdf
pbasdf

Reputation: 21536

In your cellForRowAtIndexPath method (and the other datasource/delegate methods, use indexPath.section as the index to your data array, not indexPath.row.

Or in your case, replace

NSDictionary *dict = [self.imagesa objectAtIndex:indexPath.item];

with

NSDictionary *dict = [self.imagesa objectAtIndex:indexPath.section];

Upvotes: 1

KVISH
KVISH

Reputation: 13208

If you think logically through this, the indexPath.row will always be 0 since each section has one row. Hence, the row number you are using on the array will grab the same item every time.

The only value that's increasing each time is the indexPath.section. If you use this value while iterating through the array it will lead to the different values in the corresponding array.

Upvotes: 2

Related Questions