I have implemented cell.selectionStyle = UITableViewCellSelectionStyleNone; both in:
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { }
and in:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }
But each row keeps geting blue selection first time it is clicked. How can I disable the selection entirely?
The code goes like this (The cell is custom):
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CategoryCell *cell = (CategoryCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return nil;
}
Implement it in cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//create cell
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Turn the "allowsSelection" property off for your table.
You can do this programatically or within the XIB / storyboard file.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
You should do it in cellForRowAtIndexPath
Related
I have a UITableView that is made up of a number of custom UITableViewCells.
When the table didSelectRowAtIndexPath method fires, if the indexPath.row is equal to 2, I want to update a UILabel within the cell at this row.
My thinking was to use something along the following lines;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 2){
myCustomCellView *cell = [self.essentialsTableView cellForRowAtIndexPath:indexPath];
cell.myUILabel.text = #"my text";
[tableView reloadData];
}
}
The problem here is that cellForRowAtIndexPath returns an object of type UITableViewCell, not myCustomCellView.
Can anyone advise how i might access and update properties of cells within the didSelectRowAtIndexPath method?
Define one Bool value say "secondRowIsClicked". Set it to No in your viewWillApper or viewDidAppear function.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 2)
{
secondRowIsClicked = YES;
[tableView reloadData];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Your code
if (secondRowIsClicked == YES)
{
cell.myUILabel.text = #"my text";
}
}
try with bellow code..
myCustomCellView *cell = (myCustomCellView *) [self.essentialsTableView cellForRowAtIndexPath:indexPath];
you can reload UITableViewCell with its indexPath like bellow..
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
myCustomCellView *cell = [self.essentialsTableView cellForRowAtIndexPath:indexPath];
UILabel *yourLabel=[cell.contentViews.subviews viewWithTag:indexPath.row+1];
//do whatever you want with your label;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Your code
cell.myUILabel.text = #"my text";
cell.myUILabel.tag=indexpath.row+1;
}
typecast UITableViewCell to your custom cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 2){
myCustomCellView *cell = (myCustomCellView *)[self.essentialsTableView cellForRowAtIndexPath:indexPath];
cell.myUILabel.text = #"my text";
[tableView reloadData];
}
}
you need to upadte the datasoure and den call the reload the tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 2){
//update the array which is used as the data provider in cell for row at index path.
[tableView reloadData];
}
}
myCustomCellView is a subClass of UITableViewCell.
So just typcast the cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 2){
myCustomCellView *cell = (myCustomCellView *)[self.essentialsTableView cellForRowAtIndexPath:indexPath];
cell.myUILabel.text = #"my text";
[tableView reloadData];
}
}
More info:
As per Apple coding guide lines do not user class name start with lower case letter
instead of myCustomCellView use MyCustomCellView
I Have a table view with multiple sections as follows..
I know how to set background color for the selecting a Cell / row
But i need to fill the selection to the entire section instead of individual cell selections
is it possible if yes how any solution
i hope its working....
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];
if (currentSelectedIndexPath != nil)
{
[[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor:NotSelectedCellBGColor];
}
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:SelectedCellBGColor];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell.isSelected == YES)
{
[cell setBackgroundColor:SelectedCellBGColor];
}
else
{
[cell setBackgroundColor:setBackgroundColor:NotSelectedCellBGColor];
}
}
Pass the section value from the didSelectRowAtIndexPath delegate of tableview and change the background color of the cell in cellForRowAtIndexPath dataSource method of UITableview
in Header File
NSUInteger index;
in Implemention File (.m)
-(void) viewDidLoad{
index=-1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CELL_IDENTIFIER";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section==index) {
cell.contentView.backgroundColor=[UIColor yellowColor];
}
else{
cell.contentView.backgroundColor=[UIColor clearColor];
}
cell.textLabel.text=[NSString stringWithFormat:#"Row %d Section %d",indexPath.row,indexPath.section];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
index=indexPath.section;
[tableView reloadData];
}
.h
int currentSection;
.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
currentSection = indexPath.section;
[tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( currentSection == indexPath.section ) {
cell.backgroundColor = SelectedColor;
} else {
cell.backgroundColor = OtherColor;
}
}
Hi need to create a table view with only one cell selectable like this img.
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"CellWithSwitch"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CellWithSwitch"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont systemFontOfSize:14];
if(indexPath.row == 0)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.textLabel.text = #"Sound Effects";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if(path.row == 0)
{
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
}
if you like only one row selectable you have to manage this in the methods of the table:
in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
you need to implement the selection logic. if you click on a non-selected object, you select it and deselect all others, or only the one that is currently selected (if there is one selected). If you click on a selected object, just deselect it here
dont forget to do [theTableView reloadData];
in case your screen does not update if you change something in the table's content
then to redraw you just ensure that your method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
ensures that the object which is selected is marked somehow. This way you can also do nice things like showing a toggle ON/OFF button or anything you may whish.
For a better user experience, I would like my UITableViewCell to have the default blue styling when a user taps it. Currently, there is no styling at all. Shouldn't the following this be all I need?
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView selectRowAtIndexPath:indexPath
animated:NO scrollPosition:UITableViewScrollPositionNone];
// [The rest of my code here that loads up the detail view]
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// [bunch of code]
UITableViewCell *cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:PlaceholderCellIdentifier] autorelease];
// [bunch of code that sets the text of the cell];
return cell;
}
Go to cellForRowAtIndexPath and use
cell.selectionStyle= UITableViewCellSelectionStyleBlue;
I have a basic UITableView that I want to enable the Mail.app style check marks while having no selection style. I have the following snippet:
#define UITableViewCellEditingStyleMultiSelect (3)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleMultiSelect;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
However, this will never display the check marks on selection (although the empty circles are displayed). Any ideas on how to fix it? I know it uses undocumented features but I'd really like to add support for the check marks. My actual example uses a very customized UITableViewCell and I can't enable the selection style!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
if (self.tableView.isEditing) {
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
} else {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleMultiSelect;
}
-(IBAction) switchEditing {
[self.tableView setEditing:![self.tableView isEditing]];
[self.tableView reloadData]; // force reload to reset selection style
}