iOS 7, UITableView and incorrect separator lines - iphone

I have a problem in iOS 7 with UITableView (style: UITableViewStyleGrouped) and separatorInset. At times, the separator is not visible.
Briefly, using the same table (exactly the same code) if I render the table loading directly all data (for example from a NSArray), separator lines are correct (default iOS7 style, correct inset value). If I dynamically add new rows to the same table using something like insertRowsAtIndexPaths, separator lines span the full width of the cell/screen (so, no iOS7 default style).
I tried to force 'separatorInset' using setSeparatorInset in both UITableView and in every single cell but this did not worked. If I reloadData after adding a new row, the separator lines are correctly shown.but this seems not a great solution.
Any ideas why separator is intermittently not visible?

Write this code in your ViewDidLoad method :
[tblView setSeparatorInset:UIEdgeInsetsZero];

For me the only things working is reload the previous cell, i.e. the one that is on top of the selected cell.
if (indexPath.row > 0) {
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section];
[tableView reloadRowsAtIndexPaths:#[path] withRowAnimation:UITableViewRowAnimationNone];
}

Apply your color in this method and this method into your code. this is iOS7 update.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}

Make sure clipsToBounds is set to YES for the cell, but NO for the cell's contentView. Also set cell.contentView.backgroundColor = [UIColor clearColor];

use this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
}

Related

Grouped Style UITableView doesn't clip subviews

I have a design issue with grouped uitableview, I add uiviews to the leftmost side of each cell extending little over the cell bounds and I want them to be clipped. But neither setting clip subviews in IB nor setting clipsToBounds property in code didn't help.
So far I have set clipsToBounds (for both uitableview itself and individual uitabviewcells) in a number of places but none helped:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell setClipsToBounds:TRUE];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:defaultIdentifier];
if (cell == nil)
{
cell = [self reuseTableViewCellWithIdentifier:defaultIdentifier withIndexPath:indexPath];
}
...
[cell setClipsToBounds:TRUE];
...
return cell;
}
- (void)viewDidLoad
{
...
[mainTableView setClipsToBounds:TRUE];
}
Is it possible without changing tableview style to plain?
Update: My previous answer was wrong. Here is an answer that actually shows how to do what you're asking.
https://stackoverflow.com/a/401271/350467
.......
Previous Answer
If you're adding views to the cell's contentView (which you should be, unless you have a good reason), you'll want to set cell.contentView.clipsToBounds = YES;

Xcode Setting Accessory Mark on Static Cells error

I am having a problem with the Settings page of my app. I have chosen to use static cells so I have a few table sections with 3-4 cells in each.
In viewDidLoad I load my NSUserDefaults and set the accessory marks like this:
...
}
else if ( ... my Condition3 ... ) {
indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
}
UITableViewCell* cell = [settingsTable cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
This works fine and it sets the cell with the matching setting with a Checkmark. However, this is only working for the cells that are on screen by default. Any cells which require scrolling do not contain the checkmark.
Is there a way to fix this, preferably without having to use dynamic cells?
Ahh, I finally figured it out. cellForRowAtIndexPath crashes the app so I had to do the following:
I set the cells which require a checkmark in my viewDidAppear method using the code above (in the question). I also remove all checkmarks and then add one to the selected cell in the didSelectRowAtIndexPath method as I was doing before.
In each of the two methods I set an NSString variable to the .textview.text of the selected cell and then implement this method:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.accessoryType = UITableViewCellAccessoryNone;
if ([cell.textLabel.text isEqualToString:[NSString stringWithFormat:#"%#", myFirstString]] || [cell.textLabel.text isEqualToString:[NSString stringWithFormat:#"%#", mySecondString]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
Hopefully this might help someone.
That's because when you scroll your cells are rebuilt and the checkmarks are cleaned.
You need to set it up in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

How to select a UITableView row that is not yet shown?

I'm not sure I'm phrasing my question correctly, so here's the details.
I'm using a UITableView to display the list of available fonts. When the list is dsiplayed,
only about 12 rows show at a time, so if the previously selected font is not yet show, I can't select it when first showing the view.
What I'd like is to have the cell selected and shown in the center of the list when the view appears. But since the UITableView only loads data as needed, this is the best I can get:
EDITED
I've tried this but it doesn't work (the cell is only briefly selected while scrolling):
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.textLabel setText:[fontArray objectAtIndex:indexPath.row]];
[cell.textLabel setFont:[UIFont fontWithName:[fontArray objectAtIndex:indexPath.row] size:16]];
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
//select the cell/row if it matches the current font
if([cell.textLabel.text isEqualToString:currentFontName]){
cell.selected=YES;
}
NSLog(#"returning cell %#",cell.textLabel);
return cell;
}
1 - Make your comparison using - (BOOL)isEqualToString:(NSString *)aString
1a - replace your test
if([cell.textLabel.text isEqualToString:currentFontName]){
cell.selected=YES;
}
by
cell.selected = [cell.textLabel.text isEqualToString:currentFontName];
1b - if you need to display your selected font you can do that before loading your TableView:
NSIndexPath * selFntPath = [NSIndexPath indexPathForRow: [fontArray indexOfObject: currentFontName]
inSection: 0];
[tableView scrollToRowAtIndexPath: selFntPath
atScrollPosition: UITableViewScrollPositionMiddle
animated: NO];
2 - Check that you do not unselect your cell in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath delegate method.
This is a classic behavior in most sample codes.
option: you can keep the select property for user selection and toggle a specific control (ie checkmark using accessoryType property your cell) to show a system-selected row.
This is probably the right approach but you can't test NSStrings for equality by pointer comparison. You want - (BOOL)isEqualToString:(NSString *)aString instead of ==.
I found the solution in another thread - the problem is related to reusing cells. If I do not re-use cells, then everything works properly. Re-using cells also caused problems with multiple checkmarks appearing when only one item is selected. Thanks to those who contributed.
EDIT: If I should not be answering my own questions please tell me...but also tell me the proper way to resolve the question!
EDIT 2: This thread also helped
UITableViewCell going black when selected programmatically

change color of uitableviewcell for selected cell in sectioned table

I have sectioned table view with check selection for each section(every section allows only single selection like radio button).
now what i want to do is i want to change the color of cell that user has selected(not for fraction of time color change with animation using selectedBackgroundView property of cell).
Please help.
Thanks.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
Use this code........
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor yellowColor];
}
Happy Coding....
Use this in tableview didSelectRowAtIndexPath: delegate method
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
if you want to change the selection style of a particular cell in the tableview then following code can help.. Put this in the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method and make the necessary changes
// x is the numbered location of that particular cell appearing in table view
if(indexPath.row==x)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// UITableViewCellSelectionStyleBlue or UITableViewCellSelectionStyleNone
cell.selectionStyle=UITableViewCellSelectionStyleGray;
}
when the user select a row
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
gets called. You may consider implement this method in your delegate.
If you want to change only the background when it is selected you have a view reserved for that in a cell:
UIView *selectionBackground = [[UIView alloc] init];
selectionBackground.backgroundColor = A_COLOR;
myCell.selectedBackgroundView = selectionBackground;
Where A_COLOR is a UIColor. You can set this in your cellForRowAtIndexPath.
A. If your cell's color is set to transparent (clearColor) you can simply set cell.selectedBackgroundView.backgroundColor.
For dynamic usage this can be done in -tableView:didSelectRowAtIndexPath: (you can change color each time user selects a row). Otherwise in -tableView:cellForRowAtIndexPath: - either when you setup the cell (after dequeuing or creating - to allow color changes, for example, each time you call reloadData) or when you create the table (if it wasn't dequeued - mostly for persistent color values).
B. If your cell's color is not set to transparent - set color in -tableView:didSelectRowAtIndexPath: as mentioned in most other comments. But using this method, don't forget to restore color of previously selected row.
try this:
[cell setBackgroundColor:[UIColor colorWithRed:55.0/255.0f green:255.0/255.0f blue:178.0/255.0f alpha:1]];

Issue with UITableView. After first touch, scrolls off screen, back to top, using Core Data

I'm having a problem with UITableView, populated via Core Data. The table view is within a nav controller in a tab bar controller.
It loads correctly, with the right data in the right rows. The problem is, if I touch the table (select a row, enter editing mode, etc) the table quickly scrolls down (animated) and then reappears at the top position (without animation).
This only happens on the first selection after the tableView is displayed for the first time. If I select another tab and come back, there's no issue.
Thank for your help in advance!
UPDATE
This occurs in the Simulator and Device (iPhone 4)
Here's some code
- (void)configureCell:(SavedCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Citation *citation = (Citation *)[fetchedResultsController objectAtIndexPath:indexPath];
cell.citation = citation;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *RecipeCellIdentifier = #"CitationCellIdentifier";
SavedCell *savedCell = (SavedCell *)[tableView dequeueReusableCellWithIdentifier:RecipeCellIdentifier];
if (savedCell == nil) {
savedCell = [[[SavedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RecipeCellIdentifier] autorelease];
savedCell.accessoryType = UITableViewCellAccessoryNone;
}
[self configureCell:savedCell atIndexPath:indexPath];
return savedCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Found the answer. In the nib, I had paging enabled. Disabling this seems to solve the issue. There was also a weird scrolling issue separate from the issue above.