I have a UITableView that is configured to allow multiple cells to be selected in edit mode. However, the empty white circles on the left never change to red circles with the white checkmarks inside after a cell is touched/selected.
I have read about the swipe to delete issue with allowsMultipleSelectionDuringEditing, so my setEditing:animinated method looks like this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
self.tableView.allowsMultipleSelectionDuringEditing = editing;
[super setEditing:editing animated:animated];
}
Some resources on the Net suggest setting allowsSelectionDuringEditing = NO;, but that has no effect. Also, my cell editing style is set to UITableViewCellEditingStyleDelete, and changing it does not have any effect either.
When a row is touched in edit mode, tableView:didSelectRowForIndexpath: is triggered, but as mentioned above, the UI does not reflect this.
It was, as tends to be the case, my mistake.
The problem was in my implementation of tableView:cellForRowAtIndexPath:, where I was setting the cell's selectionStyle property to UITableViewCellSelectionStyleNone. For some reason, this has the added 'benefit' of disabling the red checkmark on the left hand side in multiselection edit mode.
Setting cell.selectionStyle = UITableViewCellSelectionStyleGray; fixed the issue.
Old thread but i also had this issue, however i found the cause to be my custom cell was overriding the setSelected and setHighlighted methods without call super.
This resulted in the cells not becoming selectable.
I had a similar problem, because I was working very hard to make sure my cells never showed any selection (for a "chat bubble" style of table).
So of course this fix resulted in great big color bars on my table, and I had to find another way to get rid of them.
In your cellForRowAtIndexPath, you can set a selectedBackgroundView instead of setting the selectionStyle, and it will also enable checkboxes. The view can be your cell's background color, or clearColor, and then nothing will show up. Here's my code:
static dispatch_once_t onceToken;
static UIView * selectedBackgroundView;
dispatch_once(&onceToken, ^{
selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
selectedBackgroundView.backgroundColor = APP_CELL_BACKGROUND_COLOR;
});
cell.selectedBackgroundView = selectedBackgroundView;
I had the same problem. Ensure that tableView:shouldHighlightRowAtIndexPath: returns YES, otherwise selection will not occur, at least in iOS 7.
- (BOOL)tableView:(UITableView *)tableView
shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
Related
I am learning about UITableview on iOS and following a course online. I get the table showing fine, but the images on my cells are not all the way to the left (whereas the instructor's ones are). Here is a screenshot of the cells in question:
I don't want that gap, I want the images to be positioned right at the beggining of the cell, all the way to the left. I have done some research and it seems Apple has changed the default look of the cells between ios6 and ios7 so that now the images in cells show a little gap at the left. To get rid of it, I have tried UIEdgeInsets:
[tableView setSeparatorInset:UIEdgeInsetsZero];
and that's not working. I also have tried this approach:
cell.imageView.frame = CGRectMake( 0, 0, 50, 55 );
Nothing happens. So how would I go about it? Thanks
edit-------------------------------------------------------------------------------------------------------------------------------
Still not have found the answer to this. The solutions posted here don't work. I found this piece of code:
self.tableView.contentInset = UIEdgeInsetsMake(0, -50, 0, 0);
Which besides completely puzzling me (as the parameter affected should be the y?) I thought solved the issue by making the image on the cell appear all the way to the left, until I realised it only moved the whole view to the left (as I should have expected I guess) leaving an equal gap on the other side of the screen. All I want is for my images in the cells to appear all the way to the left of the cell as it used to be the case on previous ios. Thanks
It happens because default table content offset from left is 15, you should change it with 0.
See this once, you get idea Remove empty space before cells in UITableView
If you create custom cells. UITableViewCell have owner imageView. Change title of image in your cell.
If you use default cell, use custom cell with constraint Leading space = 0.
It is better not use default imageView of the cell. Drag and drop UIImageView from objective library, create a custom table view cell (Child class of UITableViewCell) then create and outlet of the image view just dragged.
The spacing in the UITableViewCell is because of the default TRUE returned by shouldIndentWhileEditingRowAtIndexPath method of UITableViewDelegate.
I was able to reproduce your problem by the below scenario:
UITableView is in editable mode:
self.tableView.editing = true
And you have implemented:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
To correct your code:
If you do not want to set Editing Style then you can turn off the editing mode by
self.tableView.editing = false
and remove editingStyleForRowAtIndexPath.
Else if you need editing mode then set the appropiate Editing style(UITableViewCellEditingStyleDeleteor UITableViewCellEditingStyleInsert) or simply turn the indentation off.
- (BOOL)tableView:(UITableView *)tableView
shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return FALSE;
}
You must create a custom cell, by adding a new class as a subclass of UITableViewCell. then you can design cell with autolayout and constraints which will resolve the issue.
there is a another concrete way to achieve this by creating subclass uitableviewcell (custom class).
steps to follow
create a class subclass of UITableViewCell.
in .h file create properties and outlets of UI components.
go to storyboard and add table view cell inside the tableview.
now add UI components like: imageview or button etc and set the x, y values according to.
make class of custom cell your className using identity inspector see image.
connect all outlets of UI components.
use below code uitableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *MyIdentifier = #"uniqueIdentifire";
yourCustomClassForCell *cell = (yourCustomClassForCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil){
cell = [[yourCustomClassForCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
cell.imageView.image = [imageAry objectAtIndex:indexPath.row];
}
Dont forget to give identifire by selecting your cell using storyboard Attribute inspector uniqueIdentifire to identifire property see image.
Also you can give some vertical space between cells by just to add this below code (Method only) inside customeCellClass.
- (void)setFrame:(CGRect)frame { // method to insert gap between table view cell
frame.origin.y += 6;
frame.size.height -= 2 * 6;
[super setFrame:frame];
}
You can not really change the frame of the inbuilt subviews of uitableviewcell like imageview, accessoryview. But if you create a custom tableviewcell class(even if you do not add any other subelement to it), you can change the frame of the inbuilt imageview by overriding the layoutSubviews method inside the UITableViewCell. I have tried it and it works.
#import "TableViewCell.h"
#implementation TableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void) layoutSubviews{
[super layoutSubviews];
CGRect frame = self.imageView.frame;
frame.origin.x = 0;
self.imageView.frame = frame;
}
#end
I receive a color from the server and in cellForRowAtIndexPath method I'm trying to apply this color to a view inside the cell.
The problem is that all the cells display the same color until I scroll down the table. When I start scrolling they update well their color.
I'm new on iOS and Objective-C, so if you could help me it would be appreciated, thanks.
Before scrolling:
After scrolling:
Some code: (If you want more please tell me)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CalendarDayCell *cell = (CalendarDayCell *) [tableView dequeueReusableCellWithIdentifier:#"CalendarDayCell"];
if (cell == nil) {
cell = [[CalendarDayCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CalendarDayCell"];
}
if (self.events.count) {
CalendarEvent *ce = self.events[indexPath.row];
CalendarDayCell *dayCell = (CalendarDayCell *) cell;
// ...
dayCell.viewColorBar.backgroundColor = [self colorWithHexString:ce.color];
return dayCell;
} else {
// Not important
}
}
PS: I've also tried it at willDisplayCell method with the same results.
EDIT:
Finally I figured out what were the solutions.
IDK why XCode redimensioned the color bar height to be 980px from the 50px that I specified in the nib file. And that was causing that all cells below had the same color.
I put all the views in another view, and assigned that view to the cell because some cells were not showing their color.
that's all
The problem is with reusing the cells - that means that in cellForRowAtIndex path you have to set color for every condition. This is easily reproducible with images. If you set image only sometimes, you will have to set image to none when you don't need to display it. What you need to do is to handle in the else block the control you want to change.
if(self.events.count){
dayCell.viewColorBar.backgroundColor = [self colorWithHexString:ce.color];
}
else{
dayCell.viewColorBar.backgroundColor = [UIColor clearColor];
}
Or something like that.
Are you sure that self.events.count is actually non-zero? Perhaps the tableview is being populated before self.events is setup properly.
For example, the view controller might be loading via viewDidLoad, then the UITableView, then you're setting self.events. If that's the case, self.events will be nil until the whole view/viewcontroller is loaded. That would account for it working after you start scrolling.
I have a UItableview with custom cells in it. The height of the cell changes when you select it and gives an expanding effect. However, when you you select the cell the background of all the subviews become transparent it seems. I've tried setting the cell's SelectedBackgroundView but that doesn't really affect the cells subviews.
Here are some images:
Closed:
Open:
![enter image description here][2]
This is how its supposed to look or at least does in XCode - (Sorry for the bad graphic here)
Call [tableView deselectRowAtIndexPath:indexPath animated:YES]; at didSelectRowAtIndexPath. This should solve your issue.
Edit
If you don't want to see any grey selection at all, then, in your cellForRowAtIndexPath, set the cell.selectionStyle to UITableViewCellSelectionStyleNone, like so:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Presuming that you have subclassed UITableViewCell for your custom cells, you can modify a cell's appearance when selected/deselected by overriding the setSelected method in your custom subclass. For example:
- (void) setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
//Configure the selected state here
} else {
//Configure the deselected state here
}
}
UITableViewCell changes the backgroundColor of all subviews on selection for some reason.
This might help:
DVColorLockView
If you want the normal selection behavior but want to exclude specific cell subviews, you can do something like this to lock the background color by subclassing and checking if it is locked in the backgroundColor setter.
I have a customized UITableView, the cells have a different background color (set in a custom backgroundView). However, the background color is only applied within the cell, but not extended to the inset area of the separator. As you can see in the screenshot, there is a white area to the left of the colored separator.
How can we change the color of this white line? We would like to make the line "disappear" by setting it to the same color as the cell background. Thanks!
Setting the cell's background colour to the same as the contentView's background colour seems to give the best results.
cell.backgroundColor = cell.contentView.backgroundColor;
That will handle cases where the accessoryView is the wrong colour as well.
So I just had this problem myself, and it's strange because it seems to work fine on some tableviews. I'm assuming that the grey color you are using is the background color of your tableview. If not, this solution may not work.
It seems like cells in iOS 7 don't always pick up the tableview background color when a separator inset is present, so you need to manually set the cell background to clearColor. Interestingly, setting this value in storyboards didn't work for me, I had to do it in code. Add this:
cell.backgroundColor = [UIColor clearColor];
when you configure the cell in this delegate function:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
It's the default in iOS 7. But you can change it to the way iOS 6 looks. Please try my code below. You will be amazed:
tableView.separatorColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
NSString *iosversion = [[UIDevice currentDevice] systemVersion];
int version = [iosversion intValue];
if(version>6)
{
tableView.separatorInset = UIEdgeInsetsZero;
}
Swift 3+:
tableView.separatorInset = UIEdgeInsets.zero
Use image for separator. This the only easy way to acechive your expected result. For ios 7 in xib we have edge insect property is there you can use.
Go to storyboard > Attribute Inspector > Separator Insets, then select cutom and change left coordinate from 15 to 0.
it will be Done.
In Interface builder on the UITableViewCell set the attribute 'background' to be clear (or whatever) and then on the Content View set the attribute 'background' to be clear as well and the problem should go away.
This is the only method that worked for me... (separator = None via storyboard)
I had similar issue and below code worked like charm.
[self.tableView reloadData]
reload tableview on didSelectRowAtIndexPath Method.
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView reloadData]
}
only this fixed my issue.
So, the correct number of rows shows up. On pressing any row, the correct action takes place. However, the cells themselves are nowhere to be seen.
I added a NSTimer in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
for each cell just to trace it out beyond the function - they all say that their superview is equal to the tableview in question (and is not nil, so i'm not checking nil == nil or something).
They all contain labels with the correct text.
The separator lines are being drawn.... If I change the TableView background, the whole visible area's background shows as that color.
I'm checking that each cell is neither hidden nor set to an alpha of 0.
Is there anything else I could be missing?
Are you loading from your cells from a nib file or creating programmatically?
Are you overlaying another object over your cell in the cell subview? Perhaps a subview is covering it; I can't tell, since you have not posted any code yet. Given the information you have provided, it is difficult to determine why you cannot see the cells backgroundView.
Try changing the color with
UIView *tmpView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
tmpView.backgroundColor = [UIColor blackColor];
myCell.backgroundView = tmpView;
It sounds like you have set the backgroundView of your cell to [UIColor clearColor].