codebot
codebot

Reputation: 2646

How to set section headers without repeating data in tableview iOS?

I have a table view and now I'm initialising the table like,

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [arr count]+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arr count]+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    NSUInteger row = [indexPath row];
    if (row == 0) 
    {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        return cell;
    }
    else
    {
        static NSString *CellIdentifier = @"moduleCell";
        MyTableViewCell *cell = (MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];

        if (cell == nil) 
        {

            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];

            cell.backgroundColor = [UIColor clearColor];
            cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

        }
        current = [arr objectAtIndex:indexPath.row-1];
        [cell setCellDetails:arr WithIndex:row-1 withParentView:self];
        return cell;
    }
}

When I'm doing this data repeating in my tableview. Before I do this, I did,

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

This is working but I need to set section headers like titles. So is there a way to set section headers without repeating data?

Upvotes: 0

Views: 638

Answers (4)

Himanshu Sraswat
Himanshu Sraswat

Reputation: 534

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0){//for first section

    }else if(indexPath.section == 1){//for second section

    }
}

Upvotes: 1

Rohit Chauhan
Rohit Chauhan

Reputation: 1

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        var cstmView:UIView!

        let lbl:UILabel

        cstmView = UIView()

        cstmView.frame = CGRectMake(0,0,tableView.contentSize.width,70.0)

        let logo:UIImageView = UIImageView();

        logo.image = UIImage(named: "logo_with_lightGrayColor")

        logo.frame = CGRectMake(8.0,10.0,200.0,60.0)

        lbl = UILabel()

        lbl.frame = CGRectMake(8.0,80.0,200.0,20.0)

        lbl.text = "USER NAME"

        lbl.textColor = UIColor.whiteColor()

        lbl.font = UIFont.systemFontOfSize(15.0)

        cstmView .addSubview(logo)

        cstmView.addSubview(lbl)

        return cstmView;

    }

You should implement custom header for tableview.

Upvotes: 0

Vignesh Kumar
Vignesh Kumar

Reputation: 598

You need to keep heading in Array. So, you can avoid hardcoded in the delegate method.

    NSArray *heading=@[@"Today",@"Yesterday",@"Tomorrow"];


    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
            NSString *heading = [heading objectAtIndex:section]; //  
            return heading;
    }

Upvotes: 0

sschale
sschale

Reputation: 5188

You need to implement this method:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section==0) {
        return "name";
    }else{
        //
    }
}

Upvotes: 0

Related Questions