How to add multiple buttons in accessory view of UITableView? - ios

I want to add two adjacent custom buttons in the UITableView's accessory view.
I tried doing cell.accessoryView = customButton; and then
cell.accessoryView = customButton2 .It is obvious that this replaces the previous button.
Thanks for any help!

You can add a UIView containing the two buttons as a custom accessoryView.
UIView *buttonsView = [...];
// add buttons to buttonsView
cell.accessoryView = buttonsView;
Or you can subclass UITableViewCell and add two buttons there.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIButton *buttonA = ....
UIButton *buttonB = ....
[self.contentView addSubview:buttonA];
[self.contentView addSubview:buttonB];
}
return self;
}
If you haven't done a custom UITableViewCell before this article might help.
http://code.tutsplus.com/tutorials/ios-sdk-crafting-custom-uitableview-cells--mobile-15702

You can make a custom UIView class with your buttons placed inside of it (as subviews: [self addSubview:button]). Then you can assign your custom UIView object as accessoryView of a cell.
cell.accessoryView = yourCustomView;

Related

Create Custom UITableViewCell Programmatically - Objective C

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
//[self createViews];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(#"draw rect");
[self createViews];
}
I'm creating a custom UITableViewCell. I require creating a UILabel that depends on the height of the UITableViewCell, and the height is not yet set in initWithStyle (it returns the default 44 when in reality the height of my cell varies greatly). For this reason, I call my createViews function in drawRect. This was working well, however I'm noticing that the function can be called again when I insert and delete rows.
My Question:
Does it make sense to call my createViews function inside drawRect?
You have few options here.
1. Use layoutSubviews/awakeFromNib, check whether the subviews were created, if no, create them with correct frames.
2. Use init to create views with:
Constraints
Without constraints and in layoutSubviews/awakeFromNib try to change the frame

Add subview to a custom class

I have a UITextField that I want to create a custom class on. So I created a file with a subclass of UITextField. Next, in the custom class, I want to implement a tableView. Kind of like a auto-complete textField.
I started creating it, and added the tableView like this:
[self addSubview:self.tableView];
When I run the app, the tableView is in the textField, so I can only see part of the tableView. How can I add it as a subview so I can see the full tableView?
This is what you are looking for
https://github.com/gaurvw/MPGTextField
This uitextfield subclass does what you want - it's builed for 'search' feature.
If you still want to use your own,
add tableview not to uitextfield itself, but like
[[self superview] addSubview:tableViewController.tableView];
EDIT:
you can set frame as:
CGRect frameForPresentation = [self frame];
frameForPresentation.origin.y += self.frame.size.height;
frameForPresentation.size.height = 200;
[tableViewController.tableView setFrame:frameForPresentation];
The way to add subview to uitextfield is to overload layoutSubviews method and init your tableview there:
- (void)layoutSubviews
{
[super layoutSubviews];
if (!self.tableview.superview)
{
[self setupView];
}
}
This will add the tableView as the subView of the textField.
self.tableView.frame = CGRectMake(0, CGRectGetHeight(self.bounds), CGRectGetWidth(self.bounds), YOUR_TABLE_HEIGHT);
[self addSubview:self.tableView];
self.clipsToBounds = NO;
However, a better way is to make the tableView as the textField's superView's subView, that is, the textField and the tableView should be siblings.

Trouble Adding Corner Radius to ImageView Inside TableViewCell

I've created a custom UITableViewCell class where I've created a label and imageview, and connected them via storyboard. What's the best place to apply corner radius, border width, etc. to the imageView?
I tried the following in the custom class' init method but it didn't quite work:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
productImageView.layer.borderColor = (__bridge CGColorRef)([UIColor whiteColor]);
productImageView.layer.borderWidth = 3.0f;
productImageView.layer.cornerRadius = 36.0f;
}
return self;
}
What's wrong here?
Are you doing in this in the UITableViewCell subclass? If so you need add this in awakeFromNib or prepareForReuse. When init is called the image view doesn't exist yet. Also do self.productImageView.clipsToBounds = YES;

How can I achieve UITableViewCell margins like this (pic)?

I quite like the way that padding has been added to the UITableViewCells in this app. I doubt it's possible in Storyboard, but what's the best way to achieve this?
You could easily do this in storyboard I think.
Just add a custom cell, grey background.
On that add a UIView as a subview, with a white background, and arrange the size so that it becomes a smaller rectangle inside your cell, so it gets this margin effect.
Then add your labels/imageviews on that white subview and you're good.
Anything wrong with that approach?
Feels a bit like cheating, but why not?
Using Storyboard this can be a daunting layout to achieve with UITableView on iOS 7. Programmatically it can be done fairly easily, with a bit of patience.
Anyway, given the quantity of information in the cell, I'd be tempted to use a UICollectionView with a UICollectionViewFlowLayout instead of a table. This might make things easier for you as you can set the cells size, sections margin, minimum distance between items, etc. all within Storyboard.
All you need is to make custom table cell and add specific background
#implementation CustomTableViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
<init your custom cell here>
UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
CGRect whiteRect = CGRectInset(bgView.bounds, 10, 5);
UIView *innerView = [[UIView alloc] initWithFrame:whiteRect];
innerView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
innerView.backgroundColor = [UIColor whiteColor];
[bgView addSubview:innerView];
bgView.backgroundColor = [UIColor colorWithRed:225.0/255 green:224.0/255 blue:230.0/255 alpha:1.0];
self.backgroundView = bgView;
}
return self;
}
Also here you can add self.selectedBackgroundView = selectedView in the same way to assign the specific view to your cell while it is highlighted/selected.

UITableViewCell's contentView add UIScrollView, won't perform segue

I have a subclassed UITableViewCell, if I add a UIScrollView to cell's contentView, can't perform the segue, if I comment the line, it can perform the segue.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self setup];
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self setup];
}
- (void)setup
{
UIScrollView *scrolView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
scrolView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds)+kButtonWidth, CGRectGetHeight(self.bounds));
scrolView.delegate = self;
scrolView.showsHorizontalScrollIndicator = NO;
// [self.contentView addSubview:scrolView];
}
You are adding a scroll view that is the full size of the cell in the content view, on top of all other views in the cell. It's covering them up, so no clicks get through. I suspect you want to add a scroll view as a container for your other views. I don't know how you could do that with all the standard views that the table view base class manages for you. You'd have to somehow override the behavior of the base class and change the content view into a scroll-view.
Alternatively you could do what you are doing, but then you would need to replace all the standard table view cell fields and behaviors with your own, which would be nearly impossible.
Adding a scroll view to a table view cell is probably a bad idea. The table view itself is a subclass of a scroll view, and it will be very hard/impossible to sort out which gestures are supposed to scroll the table view and which are supposed to scroll a cell. The only way I could see that working is if the cell's scroll view is limited to horizontal scrolling.

Resources