I followed UISwitch in a UITableView cell to put a UISwitch inside a tableview. Here is the code:
UISwitch *mySwitch = [[UISwitch alloc] init];
cell.accessoryView = mySwitch;
But the problem is that when I put the table into editing mode:
self.tableView.editing = YES;
The UISwitch dissapears.
Do you know how can I go around this issue?
Add UISwitch to contentView of cell.
The contentView of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the contentView so they will be positioned appropriately as the cell transitions into and out of editing mode.
[[cell contentView] addSubview:switch];
Related
I have created XIB for custom UITableViewCell. And inflated this in cellForRowAtIndexPath. I used cell accessoryType and UIButton above accessoryType view. So the button click event is not working. If I remove the accessoryType, it is working fine.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
I used for button focus,
[cell bringSubviewToFront:cell.aButton];
But this also not working.
What will I do to use accessoryType and UIButton in Custom UITableViewCell?
Thanks in Advance.
You need to add the button into the contentView of the cell. You should be able to do this in interface builder or in code by
[cell.contentView addSubview: aButton];
The contentView of a tableViewCell is where objects like UIButtons and UIImageViews are placed.
I have a tableview with my custom class for UITableViewCell, I have added some subviews to each of the UITableViewCell, now when I enable editing like this
[tableView_ setEditing:YES animated:YES];
a red delete button appears on left side of each cell and each cell is shifted towards right except the subviews which are added on the cell. This causes the button to appear on top of subviews. How to fix this? Thanks
UITableViewCell have a specific view for it's content, cell.contentView, make sure all of your addSubview calls are to this view not the cell. EX:
// good
UILabel* label = [[UILabel alloc] init];
[cell.contentView addSubView:label];
// bad
[cell addSubView:label];
In the following code, if we do [cell addSubview: someLabel] vs [cell.contentView addSubview: someLabel], they seem to work the same. Is there any difference doing one or the other? (the custom cell in the real code is adding UIImageView and UILabel) (UIView, on the other hand, doesn't have contentView, so we don't need to add subview to its contentView. UITableViewCell is a subclass of UIView by the way)
-(UITableViewCell *) tableView:(UITableView *) tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if ([tableView isEqual:self.songsTableView]){
static NSString *TableViewCellIdentifier = #"MyCells";
cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:TableViewCellIdentifier];
}
// ... some code to create a UILabel (not shown here)
[cell addSubview: someLabel]; // vs using [cell.contentView addSubView: ...]
I believe If I am not wrong, the contentView is a subview of UITableViewCell.
If you look at this page here, you can see there are actually 3 subviews in a UITableViewCell
I think by default, the Editing Control is hidden until you enter edit mode for a table in which case, the Editing Control appears (the minus button left of each row) and your contentView gets resized and pushed to the right. This is probably what gives the "proper animation" effect mentioned by the other answer.
To test the difference, try adding a subview such as UILabel with text, to the cell rather than the cell.contentView. When you add it to cell rather than cell.contentView and you enter edit mode for your table, I believe your UILabel will not resize, you will see the edit button ontop/below the minus sign button.
Placing your views in the contentView affects proper animation in and out of edit mode. Place all of your subviews in contentView when you're not subclassing, which should be all of the time unless you know what you're doing.
I have a UItableview in my IOS app with some information in it. I changed the Selected background color to clearcolor using the following code:
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
There is text and 2 images in the cell, I've build this using CGRectMake.
But When I select and hold a table cell the the images disappear behind what looks like a white background.
As you can see I'm holding the "Dacnusa sibrica" cell, how can I fix this?
If you want to disable the blue selection of cells you can also set the cell's selection mode instead of modifying the background:
cell.selectionStyle = UITableViewCellSelectionStyleNone
This simply disables the blue selection when tapping the cell but still allows the cell to be selected (and thus handled by code).
With Auto layout it is possible to define Selection=None (default is gray) in the Interface Builder.
have you used [cell.contentView addSubview:image]?
use [tableView deselectRowAtIndexPath:indexPath animated:YES];
for deselecting the cell
I have written a simple contact manager application that uses a UITableView to display the contacts. Each contact is shown as a standard UITableViewCell; custom content is created as UIButtons and UILabels that are added as subviews of the cell's contentView. My table viewController's cellForRowAtIndexPath method includes:
UIButton *emailButton;
UITableViewCell *cell =
[theTableView dequeueReusableCellWithIdentifier:#"My Identifier"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier] autorelease];
emailButton = [UIButton buttonWithType:UIButtonTypeCustom];
[emailButton setImage:emailImage forState:UIControlStateNormal];
emailButton.tag = EMAIL_BUTTON_TAG;
emailButton.frame = emailButtonFrame;
[cell.contentView addSubview:emailButton];
} else {
emailButton = (UIButton *)[cell viewWithTag:EMAIL_BUTTON_TAG];
}
... set various attributes of the cell, including the content of custom labels
... added as subviews of the contentView exactly as above
This works fine when rendering my table. But I've also added a search bar to my app, set the search bar's controller appropriately, and set the controller's delegate back to this same tableController such that the exact same cellForRowAtIndexPath method is called when performing the searches, and of course I filter the set of cells to be displayed to match the query.
What I see is that when I perform a search, all of the content that I display by setting cell.textLabel.text or cell.imageView.image shows up perfectly in the table, but the emailButton or the labels that I added as subviews of the cell's contentView don't appear. In the debugger, I can clearly see that these controls (the buttons and labels) exist at the time that cellForRowAtIndexPath is called while search filtering is going on. But the controls don't render.
I feel there must be something very subtle in the interactions between table cells and the searchView, but I'm missing it.
Setting the textLabel's text property appears to also bring the textLabel to the front. Even though the text label does not appear to overlap with any of the content view's buttons, this is causing the buttons to disappear. Forcing them to the front after the textLabel is updated makes the problem go away.
It is not clear why this behavior is only appearing in the search case and not in the normal case, but I was able to reproduce it in a simple change to the iOS "TableSearch" example.
you can check if
identifier in *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier] autorelease];
is equalt to "My Identifier" from [theTableView dequeueReusableCellWithIdentifier:#"My Identifier"]; if not you can get empty cell, which you can use it's cell.imageView and cell.textLabel but does not have the contentView subviews.