Custom Cell.accessoryView of UITableView showing only first row in iOS - ios

Hello i need to add custom cell accessoryView in my app.
Here is my code for custom cellAccessorView.
self.viewOfAccessory = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 55)];
self.lblDay.text = #"Monday";
[self.viewOfAccessory addSubview:self.lblDay];
cell.accessoryView = self.viewOfAccessory;
However it showing only in first row. Not in every row of tableView.
I want to do like following pic.
How can i do it?

A view can only be added in one place. You are using the self.viewOfAccessory property to define what you want shown and then trying to add it to all of your cells. However, self.viewOfAccessory will only show up in one place. If you add it somewhere else (i.e. another row) it will just be moved. You need to be creating separate views and adding them to each cell.

I believe the problem might be because you're calling elements as part of "self" and then adding them again in "self" (which should be the tableViewController), try this:
UIView* viewOfAccessory = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 55)];
//Get the text from lblDay label, check if the "self." is necessary
self.lblDay.text = #"Monday"; //Not sure about this line since I don't have the whole code
[viewOfAccessory addSubview: lblDay];
cell.accessoryView = viewOfAccessory;

Related

Empty UITableView still shows empty cells

I am making a Calendar app and I am showing the events from the Calendar in a UITableView.
I am using a tableFooterView to hide separator lines in a UITableView when the table is empty. I do this with the following code:
self.tableView.tableFooterView = [[UIView alloc] init];
UILabel *footerTableViewLabel = [[UILabel alloc] init];
footerTableViewLabel.text = #"No items.";
[self.tableView.tableFooterView addSubview:footerTableViewLabel];
If there are elements in the table, I hide this view, and if not I show it. This works fine if the calendar is initially empty. However, if I delete an event from Calendar.app while my app is still running in the background, when I go back to the app, all I see is a bunch of empty cells and separator, and no label saying "No items".
Any ideas?
EDIT: I show/hide the footer view using:
self.tableView.tableFooterView.hidden = NO/YES;
As far as events are considered, I do not offer the possibility to delete them directly from my app, I just detect when they are deleted from Calendar.app. Unfortunately, I cannot show the code doing this detection since it is part of a private library, however, this works fine, i.e. I jump to right parts of code when the event is deleted.
You can try this un your code to hide separators: self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Use self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; to hide the separator lines. If you want the active tableview cells to have separators, you can always add a border to the bottom of each cell manually using a function like this:
- (void)addBottomBorderWithColor:(UIColor *)color andHeight:(CGFloat) borderHeight
{
CALayer *borderBottom = [CALayer layer];
borderBottom.backgroundColor = color.CGColor;
borderBottom.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, borderHeight);
[self.layer addSublayer:borderBottom];
}
** EDIT**
If you want to add a view that indicates 'no data', try adding this as a view underneath the tableview, then just set the tableview to hidden if there is no data present.
Hope this helps
So, I finally managed to figure this out. There were to issues:
For some reason (maybe when I do [self.tableView reloadData]), self.tableView.tableFooterView would become nil and that's why I could see empty cells. Reassigning the view to it fixed this issue.
I had to update the view constraints for the footer view and the label it contains.

How to shove a label in a tableview full of buttons?

I have a table view full of buttons. A label is outside the tableview serving as heading. I want to shove it inside the table view, on top of course, without changing it to button. In other words I want to move it inside the tableview, but on top and not as a button,as a heading.
The label in question says "Select all things that apply to your home"
For code please see Set title labels of buttons in table view from an array full of strings
For Screenshot pls visit http://i60.tinypic.com/2qmf4wl.png
How about this?
UIView *headerView;
UILabel *labelView;
labelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
labelView.font = [UIFont fontWithName:#"Trebuchet MS" size:22.0];
labelView.backgroundColor = [UIColor clearColor];
labelView.text = #"* Your important note here.";
labelView.textColor = [UIColor blackColor];
[headerView addSubview:labelView];
yourTableView.tableHeaderView = headerView;
It's hard to add a label to a tableviewcontroller. Two options I can think of:
Make "Select all things that apply to your home" your navigation item title. (you can do this right in the storyboard)
Place a table view in a normal view controller and leave enough space on top for a label. (again, you can drag a normal table view into a view controller in a storyboard)
Hope it helps!
you can creat a subclass of UITableViewCell and custom the cell for the tabe

2 UIBarButtonItem's using the same custom view only added once

I want to add a thin line between items in my UIToolBar so I'm creating a UIBarButtonItem with a custom view like so:
UILabel *separatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 44)];
separatorLabel.backgroundColor = [UIColor colorWithRGB:0xe5edec];
UIBarButtonItem *separator = [[UIBarButtonItem alloc] initWithCustomView:separatorLabel];
Then I add my separator to the items array:
[items addObjectsFromArray:[NSMutableArray arrayWithObjects:someButton1, separator, somebutton2, separator, someButton3, nil]];
I thought this would add 2 separators to my toolbar but it only adds the one at the end. Why is this?
I can fix this by creating 2 duplicate labels and UIBarButtonItem's, but is there any other better way?
Any given instance of UIView can only appear in once in the view hierarchy. If you think about the APIs defined on UIView, this is fairly obvious. For example, UIView has a frame property which defines it's location in the superview. The frame property wouldn't make sense if the viewed appeared in two places.
So you need multiple instances. You can streamline your code by defining a method that creates separators:
- (UILabel *)newSeparator
{
UILabel *separatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 44)];
separatorLabel.backgroundColor = [UIColor colorWithRGB:0xe5edec];
UIBarButtonItem *separator = [[UIBarButtonItem alloc] initWithCustomView:separatorLabel];
return separator;
}
And then you can add your items like this:
[items addObjectsFromArray:#[button1, [self newSeparator], button2, [self newSeparator]];
Also, you don't need to use UILabel if you're only displaying a background color. You can just use UIView.
Yes,you just created only one UIBarButtonItem object,so it showed one.
I think the better way is creating a UIBarButtonItem subclass with custom label,then create two objects of the subclass.
I hope my answer can help you.

Duplicated uiview added programmatically

I'm currently making a photo decoration app. The *holderView is the sticker that has been chosen by user. Whenever I try to load a photo from photo library or take a photo and then load back to this page, additional *holderView added programmatically, which is the sticker that I've previously chosen before taking a photo, duplicated sticker appears after that, which is not what I want it to be.
How should I write the code for not letting this happen?
Thanks a lot.
- (void)viewWillAppear:(BOOL)animated {
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, _imagePicker.selectedImage.size.width, _imagePicker.selectedImage.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageView setImage:_imagePicker.selectedImage];
[holderView addSubview:imageView];
...
}
Your problem seems to be that your using the viewWillAppear method instead of the viewDidLoad. This will cause multiple "imageViews" because your adding a new one every time you hide then show the viewController it's presented in. what you want to do is move the creation of the imageView (if there really is suppose to only be 1) to the viewDidLoad method and make that imageView accessible to the entire class, then in the viewWillApear simply change the image inside the imageView to the newly selected one.

Transparent background for the Group Table Cell

For group table cell, I fall into this problem.
cell.backgroundColor=[UIColor clearColor]
make the cell bg black. It works for normal cell, not for group table cell.
I want to add some button, e.g. like the detail view of iPhone contact with transparent background.
If anybody face the problem, I got a solution, set a transparent view as a background view of the cell. Then it becomes totally transparent. Then you can add more view or customize the cell.
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
messageCell.backgroundView = backView;
messageCell.contentView.layer.cornerRadius = 10.0;
messageCell.contentView.layer.borderWidth = 1.0f;
messageCell.contentView.layer.borderColor = [[Settings getInstance] colorFrameBorder].CGColor;
messageCell.selectionStyle = UITableViewCellSelectionStyleNone;
return messageCell;
This solution was quoted in one of the StackOverflow question, which I cant remember.
I have also found that, its easy to add a transparent view in the table header or footer. The button down the contact details are probably added in a footer view.
From the looks of it I'd say you are setting a background image to your cell. You can see it at each cell on the right side, there are the stripes from your view background.
Remove the cell's background and you should be fine.
I found the solution from this answer here by setting cell's backgroundView
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
I would take Charles's answer one step further and do the following
self.myTableView.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];

Resources