Pooja Borkar
Pooja Borkar

Reputation: 55

how to create custom cell with label & textfield in objective c

I have taken section with title let say

Monday,Tuesday,Wednesday......Sunday etc.also I have added "+" button after section title & added action on that button

Below is code and screenshot.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [SectionArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==0) {
        return 0;
    }else{
            return 3;
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [SectionArray objectAtIndex:section];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGRect frame = tableView.frame;
    UIView *headerView;
    if(section == 0 || section == 7) {

    }else{
    UIButton *addButton=[UIButton buttonWithType:UIButtonTypeContactAdd];
    addButton.frame =CGRectMake(frame.size.width-60, 5, 50,30);
    addButton.titleLabel.text = @"+";
        addButton.tag =section;
   // addButton.backgroundColor = [UIColor grayColor];
        [addButton addTarget:self action:@selector(AddTimeSlot:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(30, 10, 100, 30)];
    title.text = [SectionArray objectAtIndex:section];

    headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [headerView addSubview:title];
    [headerView addSubview:addButton];
    }
    return headerView;

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

    NSString *cellIdent = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];

    if(cell == nil){

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
    }
    return cell;
}

this is i have made format of design

now i have to create cell when i click on "+" button,so please help me .

Upvotes: 2

Views: 1567

Answers (2)

Bharat Modi
Bharat Modi

Reputation: 4180

You can do that as below,

First you have to use an array for the data source that would be manipulated dynamically.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==0) {
        return 0;
    }else{
        return numberOfRowNeedToDisplay; //Take global int variable
    }
}

In your button action AddTimeSlot:

-(void)addTimeSlot:(id)sender {

//If you want to add cell to section
NSMutableArray *arrayIndexPathsToBeAdded = [[NSMutableArray alloc] init];

    for (int i = 0; i < <numberOfCellsYouWantToAdd>; i++) {

       [arrayIndexPathsToBeAdded addObject:[NSIndexPath indexPathForRow:i inSection:view.tag]];

    }

    numberOfRowNeedToDisplay = <numberOfCellsYouWantToAdd>;

    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:arrayIndexPathsToBeAdded withRowAnimation:UITableViewRowAnimationRight];

    [self.tableView endUpdates];

//If you want to remove cells
NSMutableArray *arrayIndexPathsToBeRemoved = [[NSMutableArray alloc] init];

    for (int i = 0; i < <numberOfCellsYouWantToRemove>; i++) {
        [arrayIndexPathsToBeRemoved addObject:[NSIndexPath indexPathForRow:i inSection:sectionIndexToBeExpanded]];
    }

    numberOfRowNeedToDisplay = <numberOfCellsYouWantToRemove>;

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:arrayIndexPathsToBeRemoved withRowAnimation:UITableViewRowAnimationLeft];

}

This is what i have had done:

enter image description here

Upvotes: 3

Chathurka
Chathurka

Reputation: 625

Please read commented text.

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

  /** should be change in dynamically
    maintain array for every section rows
    return row count accordint to section
   **/
}

#pragma mark - Private methods

- (void)AddTimeSlot:(UIButton *)sender {

    int sectionNo = sender.tag;
    // Get you section rowArray(DATASOURCE) and insert you detaisl
    // then add UI

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

    [tableView beginUpdates];
    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [tableView endUpdates];
}

Upvotes: 0

Related Questions