multiple selection in uitableview - dequeueReusableCellWithIdentifier is causing issue - iphone

//selected rows is mutablearray
if (aCell.accessoryType == UITableViewCellAccessoryCheckmark) {
[selectedRows addObject:indexPath];
}
Problem here is when I scroll uitableview, all selection is messed up. What is the best way keep track of selected cells? I also do not want to use checkmark I just want to change background color of selected rows.

If you want to change the selected color of the selected cell.cell.selectedcolor = [uicolor blackcolcor];
or you can use this method for change background color of the custom tableeview cell
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];

Related

How to change the cell with selected color when pressing and holding on to the button

I have an application in which I have two buttons on a tableView and the remaining as cells. I have been using backgroundimage view for my cells. like this `
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"cell_with_arrow.png"]];
[cell setBackgroundView:img];
[img release];
then i am changing the selected background color as
UIView *violetColor = [[UIView alloc] init];
violetColor.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"trans.png"]];
cell.selectedBackgroundView = violetColor;
[violetColor release];
everything working fine for me in cells,but in my two cells I have added two buttons. when selecting and holding this button also I need to change my selected background color of the background cell in which that button is added. I was done it but the problem the cell background color is changing only when I am releasing my tap on the button. not when I am holding on to that button.I need to change that also when i am holding on to it. I am doing now like this
UIView *contentView = (UIView *)[sender superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
in the button action. But in the cells when I am holding the color changes, not with button why? can anybody help me?
You can use UIControlEventTouchDown event of the button to achieve your requirement instead of UIControlEventTouchUpInside.
Hope this is helpful to you.

Customize a table's cell color when the user is returning to the table

I got an easy one for you. I'm trying to keep the default behavior when a user is coming back from a view after selecting a tableviewcell. Typically the cell will remain blue for a moment to show the user where that selected row was, but I forget how to customize that. Lets say for simplistic purposes all my cells are green by default and become red when you select them.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (selected) {
self.backgroundColor = [UIColor redColor];
} else {
self.backgroundColor = [UIColor greenColor];
}
}
When the user selects the back button, the cell does not show the user their last selected cell, red in this case. I remember there being a variable I could use to customize that in the controller, but I can't remember what it was. Can somebody point me in the right direction?
Thanks!
If you only want to change the color of the background cell you can create a new view, set it's background color and assign that to the cells selected background view.
UIView *cellSelectedBackgroundView = [[UIView alloc] init];
[cellSelectedBackgroundView setBackgroundColor:[UIColor someColor]];
[cell setSelectedBackgroundView:cellSelectedBackgroundCView];
The property and method I was looking for was:
self.clearsSelectionOnViewWillAppear = YES;
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
Thanks for those who replied.
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];

display label with UITableViewCellAccessoryDisclosureIndicator

I hope display label as below
So I used the codes below to display label with UITableViewCellAccessoryDisclosureIndicator
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero// cellframe //SimpleTableIdentifier1
reuseIdentifier:SimpleTableIdentifier1] autorelease];
cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;//
UILabel *labelCell =[ [[UILabel alloc]init] autorelease];
[labelCell setTag:11101];
labelCell.frame = CGRectMake(180.9f,15.0f,60.0f,23.0f) ;
[labelCell setBackgroundColor:[UIColor clearColor]];
labelCell.textAlignment=UITextAlignmentRight;
[labelCell setFont:[UIFont systemFontOfSize:13.0]];
[labelCell setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:labelCell];
UITableViewCellAccessoryDisclosureIndicator will display correctly
but the labelCell does not display, except I tap the row of the UITasbleViewCell
Welcome any comment
change the frame property of your UILabel,
try with changing value for x and y.
labelCell.frame = CGRectMake(180.9f,15.0f,60.0f,23.0f) ;
You could also consider to implement the accessory using accessoryView.
Implement a Custom Accessory View For UITableView in iPhone
Somehow I think doing this,
[cell.contentView bringSubviewToFront:labelCell];
will help but since there are other subviews in the table cell you will have to deal with their layout as well. Rather I suggest you create a new custom UITableViewCell subclass.

Customize UITableView section border color for each section

I am using a UITableView with grouping. My first group I have built a custom cell with no background color, and I would also like to remove the border around the cell. How can I do this just for one section?
For my first cell, I would like to set the border/seperator style to [UIColor clearColor].
EDIT: Apple does this in their contacts app, and I wanted to design something similar.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0){
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}
}
I believe what Apple uses for its contact heading is tableView:viewForHeaderInSection: for section 0, instead of a cell in the first section. You can still specify a transparent background and the pinstripe will show behind it. The border will not be there as grouped table view section headers do not have borders.
Whether you built your custom cell in Interface Builder or using code, it should be a fairly trivial task to migrate those views from a UITableViewCell to a UIView for use with tableView:viewForHeaderInSection:.
I don't know if you'll be able to keep the Details title for, say, section 1, unless you make a label containing that word and add it to the section 0 header view manually.
If i understood your question, in the cellForRawIndexPath you should add-
if(indexPath.section==0){
//set the style you wish to add only to the first section
}
good luck
EDIT
I use this function for that -
+(void)setTransparentBgToCell:(UITableViewCell*)cell{
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
backgroundView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backgroundView;
[backgroundView release];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
shani

Removing cell borders from a section of grouped-style UITableView

I have a UITableViewController initialized with the grouped style and having multiple sections. For one of these sections, I'd like its constituent cells to be completely transparent and have no border. I plan to assign a custom view for every row in this section, but having that custom view surrounded by the grouped table cell looks bad :(
The following makes the background color of a cell black instead of transparent... And I still don't know how to get rid of the border.
cell.backgroundColor = [UIColor clearColor];
Any pointers? Thanks!
NOTE: This doesn't appear to be working in iOS7 and above. For iOS7 try this answer.
For iOS6 and below, to remove the grouped background from a cell in a grouped table view cell:
This didn't work
cell.backgroundView = nil; // Did Not Work
This did
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
If you have moved to ARC (I've heard this works, but haven't tested it)
cell.backgroundView = [UIView new];
You have to actually set
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
to remove the border of cells.
The following hack works in iOS 7 – for now. :)
Subclass UITableViewCell, and use this cell for the section that shouldn't have separators.
Override the addSubview method in your cell subclass:
-(void)addSubview:(UIView *)view
{
// The separator has a height of 0.5pt on a retina display and 1pt on non-retina.
// Prevent subviews with this height from being added.
if (CGRectGetHeight(view.frame)*[UIScreen mainScreen].scale == 1)
{
return;
}
[super addSubview:view];
}
This is what worked for with having a Grouped style table
[tableView setSeparatorColor:[UIColor clearColor]];
This code worked for me :)
[self.tableView setSeparatorColor:[UIColor clearColor]];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Set the backgroundView of the cell to nil. For a grouped table, the cell image is part of that view.
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
Try using tableView.separatorColor = [UIColor clearColor];
And, don't use tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
I tested with both, if style is none, making the section borders invisible is not working, but instead just change its color, and section border will appear to be none.
iOS seems to be differentiating making an object none and making an object transparent
cell.backgroundView = [UIView new];
Works like a charm! Tested! iOS6
As of iOS 8, setting the separator attribute to none works as well.
Setting a content view also gets rid of the border. Set your custom view to cell.contentView.
The easiest way to remove cell borders from a section of grouped-style UITableView:
[tableViewOutlet setBackgroundView:nil];
in the viewDidLoad method.
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
cell.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:imageView];
If you have a custom UITableCellView then you can add the following method to your view to remove the background view.
- (void)setBackgroundView:(UIView *)backgroundView
{
// We don't want background views for this cell.
[super setBackgroundView:nil];
}
I just thought I would convert my comment to #Intentss into an answer, because it maybe useful for those, using his solution.
Using iOS6.1 with a grouped UITabelView, using ARC:
[tableView setSeparatorColor:[UIColor clearColor]];
Does not work
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
Does work