Grouped Style UITableView doesn't clip subviews - iphone

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;

Related

iOS 7, UITableView and incorrect separator lines

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];
}
}

iOS 6: UITableView Edit and Autoresizing

Hi Could someone please help me to understand how to layout table cell's components automatically while editing in iOS 6.0? I have set AutoLayout FALSE for UITableViewCell Autosizing option set to top right in Attributes Inspector! Cell's right side imageviews are overlapping by delete buttons. Please refer attached image. Following is the code for this. Is there anyway I can fix this issue?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = (PlayerCell *)[tableView dequeueReusableCellWithIdentifier:#"PlayerCell"];
Player *player = [self.players objectAtIndex:indexPath.row];
cell.nameLabel.text = player.name;
cell.gameLabel.text = player.game;
cell.ratingImageView.image = [self imageForRating:player.rating];
return cell;
}
- (UIImage *)imageForRating:(int)rating
{
switch (rating)
{
case 1: return [UIImage imageNamed:#"1StarSmall.png"];
case 2: return [UIImage imageNamed:#"2StarsSmall.png"];
case 3: return [UIImage imageNamed:#"3StarsSmall.png"];
case 4: return [UIImage imageNamed:#"4StarsSmall.png"];
case 5: return [UIImage imageNamed:#"5StarsSmall.png"];
}
return nil;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.players removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
I have added following delegate methods and it works fine when I swipe the cell.
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = (PlayerCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.ratingImageView.hidden = YES;
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = (PlayerCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.ratingImageView.hidden = NO;
}
...but this methods doesn't get invoked when I press editButtonItem Button! That's strange! I am missing something. I have added following method that gets invoked when Edit/Done button pressed but there is no way to detect the cell that will be selected for editing!
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[super setEditing:editing animated:animate];
if(editing)
{
NSLog(#"editMode on");
}
else
{
NSLog(#"Done leave editmode");
}
}
When user clicks left round button, is there anyway to add selector on that button click and get the cell index?
Your ratingImageView uses autoresizing masks to resize and position itself when you are not using AutoLayout. By default this means that the distance to the left and top is fixed and the size won't change.
When you toggle a cell to edit the contentView moved and resizes but your ratingImageView stays fixed to the top and left. You can visualize this by temporarily (for learning purposes) set a background color to the cells contentView and see how it resizes as you edit and delete the cell.
What you want is for your rating view to stay a fixed distance from the right edge instead of the left. You can either change this in InterfaceBuilder (where you do your XIBs or Storyboard) or in code by setting the autoresizingMask property of the ratingImageView.
Go to this tab
Change the autosizing like this
Or do it in code
// in code you specify what should be flexible instead of what should be fixed...
[ratingImageView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];

how to remove invisible tableview cell's downloaded image . in ios

I want to remove downloaded image in invisible tableview cell when I scrolled tableview.
So I set a tag
- (void)appImageDidLoad:(NSIndexPath *)indexPath
{
int row=(int)indexPath
UITableViewCell *cell;
cell=[self.gamesTableView cellForRowAtIndexPath:indexPath];
OneItemCell *cell1=(OneItemCell*) cell; //custom cell
cell1.imageview.tag= row+100;
}
I set remove downloaded image when selected row is last row cell(=add cell).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(row==nArrayCount)
{
if(row>19)
{
for(int i=1;i<row-8;i++)
{
[[self.gamesTableView viewWithTag:i+100] removeFromSuperview];
}
}
}
but it is not working . because [[self.gamesTableView viewWithTag:i+100] is NULL.
another way I set a key to cell's imageView by NSMutableDictionary(=imageRemoveInProgress)
- (void)appImageDidLoad:(NSIndexPath *)indexPath
{ .
.
[imageRemoveInProgress setObject:cell1.imageView forKey:[NSNumber numberWithInt:row+100]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ .
.
[[imageRemoveInProgress objectForKey:[NSNumber numberWithInt:row+100]] removeFromSuperview];
}
it is not working too . because[[imageRemoveInProgress objectForKey:[NSNumber numberWithInt:row+100]] is NULL too.
[[imageRemoveInProgress objectForKey:[NSNumber numberWithInt:row+100]] has value in appImageDidLoad.
but In - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
it has NULL.
why do taged and seted key value cell's imageView become NULL?
how to remove tableViewcell's imageview?
I don't know the exact way, But I can provide a hint to do it
you can use something like this
NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
visiblePaths is an array of indexPaths of cells that are currently visible, you can check whether the cell for which you want to remove the image lies in this array or not.

UITableViewCell with 2 accessory types

I would like to make a UITableViewCell with one label and two accessory types:
Unselected cells should display a UITableViewCellAccessoryDetailDisclosureButton accessory.
The selected cell should display both the UITableViewCellAccessoryDisclosureIndicator and the UITableViewCellAccessoryDetailDisclosureButton accessories.
The only way I know how to do this is by using an image for the selected cell's accessory view. Is there an alternative way to do this?
Make a custom UITableViewCell (numerous tutorials and examples online and also in the documentation).
In your
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath //selectedIndex is a property
}
Then in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//usual cell stuff
if(indexPath == selectedIndex)
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
else
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}
So the trick is just to keep a reference to the selected cell and set the indicator accordingly.
Note that you might want to test if the cell is already selected before setting the selectedIndex, in that case you should set selectedIndex = nil.

How to make UITableViewCell highlighted state persist

I have a UITableviewCell. When a user clicks the cell, Im saving the indexpath and then calling the cellforrowAtIndexpath method to get the cell and then call the SetHighlighted:TRUE on that cell.
This works fine but the problem is when I scroll up and down the tableview, the selected cell when reappears, is not highlighted. How do I make the highlighted blue color persist so the user can visually see their selection even after scrolling the table up or down?
Thanks
save the indexpath of the selected cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedIndexPath = indexPath;
}
and compare in tableVIew:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// configure cell
if ([indexPath isEqual:self.selectedIndexPath]) {
[cell setHighlighted:YES];
}
else {
[cell setHighlighted:NO];
}
return cell;
}
However, keep in mind that apple discourages the use of the cell highlight state to indicate selected cell. You should probably use cell.accessoryType = UITableViewCellAccessoryCheckmark;