How to update a property of a custom UITableViewCell on didSelectRowAtIndex? - iphone

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

Related

How to set selection backGround for the entire section in u itable view

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

Cannot disable highlighting the first time UITableView selected?

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

How to create a table view with only one row selectable

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.

How to get cell text based on indexPath?

I have a UITabBarController with more than 5 UITabBarItems so the moreNavigationController is available.
In my UITabBarController Delegate I do the following:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//do some stuff
//...
UITableView *moreView = (UITableView *)self.tabBarController.moreNavigationController.topViewController.view;
moreView.delegate = self;
}
I want to implement a UITableViewDelegate so I can capture the row that was selected, set a custom view property and then push the view controller:
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//how can I get the text of the cell here?
}
I need to get the text of a cell when the user taps on a row. How can I accomplish this?
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//how can I get the text of the cell here?
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
}
A better Solution is to maintain Array of cell and use it directly here
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Service *service = [self.nearMeArray objectAtIndex:indexPath.row];
cell.textLabel.text = service.name;
cell.detailTextLabel.text = service.description;
if(![self.mutArray containsObject:cell])
[self.mutArray insertObject:cell atIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.mutArray objectAtIndex:indexPath.row];
NSString *str = cell.textLabel.text;
}
Swift 5
You can always get your cell from UITableVIew or UICollectionView based on IndexPath like this:
tableView.cellForItem(at: indexPath)
collectionView.cellForItem(at: indexPath)

When I tap the first cell in my tableview didSelectRowAtIndexPath is not triggered

When I tap the first cell in my tableview didSelectRowAtIndexPath is not triggered... Has anyone seem this and/or have any clue as to why this happens? All the other cells in my table work just fine and I put a breakpoint in my code in didSelectRowAtIndexPath the app does not go into that code if the first cell is tapped but will go into the code if the any other cell is tapped.
Here is some code:
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = 0;
return row;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
if (row == 0)
return nil;
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSUInteger row = [indexPath row];
//NSString *rowValue = [poolListData objectAtIndex:row];
int newRow = [indexPath row];
int oldRow = [lastIndexPathForPoolList row];
if (newRow != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPathForPoolList];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPathForPoolList = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (NSInteger)tableViewForDelete:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [poolListData count];
}
- (UITableViewCell *)tableViewtableViewForDelete:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DeleteMeCellIdentifier = #"DeleteMeCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DeleteMeCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:DeleteMeCellIdentifier] autorelease];
}
NSInteger row = [indexPath row];
cell.text = [self.poolListData objectAtIndex:row];
return cell;
}
#pragma mark -
#pragma mark Table View Delegate Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
[self.poolListData removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Thank you for your time and any help you can give me!
I think that your return nil in willSelectRowForIndexPath for the first row. This will prevent the row from being selected.