3

I am fairly new to Objective C programming, and have a UITableView setup with a custom cell. I want to make it so a user can touch a button that will add another cell, and this button will appear in the last cell only. Currently, it is not showing up. Here is the code that I am using. I have created the button within the custom cell, and used "setHidden:YES" to hide it within the cell itself. I am trying "setHidden:NO" to make the button appear in the TableView code, but it is not working. I thought maybe it had something to do with reloading the cell, but I am not sure if I am going in the right direction with this or not. I would appreciate any help on this, thanks.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{workoutTableViewCell *cell = (workoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.addButton setTitle:(NSString *)indexPath forState:UIControlStateApplication];
[cell.textLabel setText:[NSString stringWithFormat:@"Row %i in Section %i", [indexPath row], [indexPath section]]];

NSInteger sectionsAmount = [tableView numberOfSections];
NSInteger rowsAmount = [tableView numberOfRowsInSection:[indexPath section]];

if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) {
    NSLog(@"Reached last cell");
    [cell.addButton setHidden:NO];
    if (lc == NO)
    {[[self tableView] reloadData];
        lc = YES;
    }
}

 return cell;
 }
J.J.
  • 1,128
  • 2
  • 23
  • 62

3 Answers3

7

Following UITableViewDataSource method will help you to return exact number of rows available in section. Here you need to return additional as you want to have last as your button.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return yourRowCount + 1;
}

Now in folowing method you will check row number using indexpath.row as

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *lastCellIdentifier = @"LastCellIdentifier";
    static NSString *workoutCellIdentifier = @"WorkoutCellIdentifier";

    if(indexPath.row==(yourRowCount+1)){ //This is last cell so create normal cell
        UITableViewCell *lastcell = [tableView dequeueReusableCellWithIdentifier:lastCellIdentifier];
        if(!lastcell){
            lastcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:lastCellIdentifier];
            CGRect frame = CGRectMake(0,0,320,40);
            UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [aButton addTarget:self action:@selector(btnAddRowTapped:) forControlEvents:UIControlEventTouchUpInside];
            aButton.frame = frame;
            [lastcell addSubview:aButton];
        }
        return lastcell;
    } else { //This is normal cells so create your worktouttablecell
        workoutTableViewCell *cell = (workoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:workoutCellIdentifier];
        //Configure your cell

    }
}

Or you can do like create UIView programatically and set it as FooterView as suggested by @student in comment code would look like,

CGRect frame = CGRectMake(0,0,320,40);
UIView *footerView = [[UIView alloc] initWithFrame:frame];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton addTarget:self action:@selector(btnAddRowTapped:) forControlEvents:UIControlEventTouchUpInside];
aButton.frame = frame;
[footerView addSubView:aButton];
[yourTableNmae setTableFooterView:footerView];

Declare method as follow

-(IBAction)btnAddRowTapped:(id)sender{
    NSLog(@"Your button tapped");
}
Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
  • please edit cellForRowAtIndexPath method in your answer also. It'll help him more. Bcause he has done wrong allocation of cell and not implemented else condition. Thanks @Janak – Yogendra Jul 04 '14 at 04:55
0
if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) {
    NSLog(@"Reached last cell");
    [cell.addButton setHidden:NO];
} else { 
    [cell.addButton setHidden:YES];
}

Replace this code in your program.

0

If you know your number of cells in the uitable and you wish to just know when the last row will appear, you could implement the following delegate method

  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

this method tells the delegate table view is about to draw cell for particular row, simple compare your row with table rowcount.

Umar Farooq
  • 735
  • 1
  • 10
  • 19