Roman Simenok
Roman Simenok

Reputation: 560

Add Button to separate cell

I need to add button to separate cells

Button in yellow circle

Button in yellow circle

i can add to all cells like [cell addSubview:myButt]; but it works to all cells, i need only for 5, 6, 7, 8 cells;

Do i need to write a custom UITableViewCell?

And that line(black arrows), as i understand it is Section, how can i create it with no title, with line(image) and in middle of the table? Thank's to all.

P.S. Sorry for my English

Upvotes: 0

Views: 93

Answers (3)

Samkit Jain
Samkit Jain

Reputation: 2523

Just add button in cellforrow method :

If (indexpath.row == 3 || indexpath.row == 4 || indexpath.row == 5 || indexpath.row == 3) 
{
   // add button here
   // tag button here
   Button.tag = indexpath.row ;
}

Upvotes: -1

Sudershan shastri
Sudershan shastri

Reputation: 720

You can hide your section header like this :

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0f;
    // or 1.0 as per your line
}

For buttons on your cells, you have to create a custom cell Class and then use that in cellForRowAtIndexPath method. You can also use tags for this. You can do it like this:

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:"Cell"];
if (cell == nil) {
    cell = [[CustomCell  alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
if(indexPath.section == 1)
{
  if(indexPath.row>=5 && indexPath.row <=8)
   {
    //adding buttons to custom cell
   }
}
}

Upvotes: 4

jbouaziz
jbouaziz

Reputation: 1493

You could use the tag property of UIView to keep track of your button but I'd really recommend to use a custom UITableViewCell. With that being said, below is the code you could use. It is optimized to not add the same button over and over but just when you need it, and hide it in cases you don't need it.

static int kButtonViewTag = 3294802;
UIButton *button = [cell.contentView viewWithTag:kButtonViewTag];
BOOL shouldDisplayButton = indexpath.row == 5 || indexpath.row == 6 || indexpath.row == 7 || indexpath.row == 8;

// If the button should be displayed and is not
//
if (shouldDisplayButton)  {

    // Add button here
    //
    if (!button) {
        button = // Init your button
        button.tag = kButtonViewTag;
        [cell.contentView addSubview:button]
    }
    else if (button.isHidden) {
        button.hidden = NO;
    }
}


// If the button should not be displayed but is
//
else if (!shouldDisplayButton && button && !button.isHidden) {
    button.hidden = NO;
}

And then for your section:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGFloat tableWidth = tableView.frame.size.width;
    CGFloat padding = 10; // Add some padding

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(padding, 0, tableWidth - padding * 2, 1)];
    [view setBackgroundColor:[UIColor greyColor]; //your background color...
    return view;
}

Upvotes: 1

Related Questions