I have a custoum cell with an image , and i want to change the image only when cell is selected.
This i was trying :
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (!tableView.tag==0) {
TableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell=[[TableViewCell2 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
// cell.cam.image = [UIImage imageNamed:#"cam_normal.png"];
cell.cam.highlightedImage = [UIImage imageNamed:#"cam_selected.png"];
}
}
but the image is not changing.
This is working for a normal cell.
Another try was this in cellForRowAtIndexPath
if (cell.selected==TRUE) {
cell.cam.image=[UIImage imageNamed:#"cam_selected.png"];
}
You already get the cell as a parameter but fetch another one when using dequeueReusableCellWithIdentifier!
Try something like this:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!tableView.tag==0)
{
TableViewCell2 *changeImageCell = (TableViewCell2*) cell;
changeImageCell.cam.highlightedImage = [UIImage imageNamed:#"cam_selected.png"];
}
}
You have to do 2 things:
1. Change the image immediately when the cell is selected.
To do this you must first implement UITableViewCell delegate. But since you already have tableViewWillDisplayCel:forRowAtIndexPath:, I assume that you already have the delegate implemented.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell2 *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.cam.highlightedImage = [UIImage imageNamed:#"cam_selected"];
}
You could change the image back when the user deselect the cell.
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell2 *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.cam.highlightedImage = [UIImage imageNamed:#"cam_unselected"];
}
2. Persist the selection when the cell is reused.
Again, when the cell is reused, you have to check whether it is selected or not. Then assign the image accordingly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell2 *cell = [tableView dequeReusableCellWithIdentifier:#"cell"];
BOOL isRowSelected = [indexPath isEqaul:[tableView indexPathForSelectedRow]];
if (isRowSelected) {
cell.cam.highlightedImage = [UIImage imageNamed:#"cam_selected"];
} else {
cell.cam.hightlightedImage = [UIImage imageNamed:#"cam_unselected"];
}
}
Two things I notice here. First you never use true or TRUE in Objective-C. You use BOOL with YES and NO. Second, you rarely need to use dequeReusableCellWithIdentifier: outside tableView:cellForRowAtIndexPath:. Why did you do it there?
Related
I am new in iOS development and i want to know when i click on row of table, my data which are shown in table view should be select on label. My label has name City.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [arrayname objectAtIndex:indexPath.row];
return cell;
}
In viewDidLoad add this array
dataArr=[[NSMutableArray alloc]initWithObjects:#"Hyderabad",#"Bangalore",#"Chennai",#"Pune",#"Mumbai", nil];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reUseid=#"cellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reUseid];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reUseid];
}
cell.textLabel.text = dataArr[indexPath.row];
return cell;
}
Here is TableView didSelectRowAtIndexPath method.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"selected is %#",[dataArr objectAtIndex:indexPath.row]);
[self performSegueWithIdentifier:#"viewToDescriptionSegue" sender:self];
}
in this method you got indexpath from that you can get selected row like indexpath.row and from that you can get object from array.
There is a UITableViewDelegate method named didSelectRowAtIndexPath which get called up when user select any row. Get the value from your array at indexPath.row and set the text on your label.
Implement the below code on your project:-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *name = [arrayname objectAtIndex:indexPath.row];
[yourLabelInstance setText:name];
}
This is a delegate method of UITableView which is called when you tap on cell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
city.text = [arrayname objectAtIndex:indexPath.row];
}
Below is a delegate method of UITableView which is called when you touch/select any row in table.
Here, using indexPath parameter you can access currently selected row index. You can access data from datasource array having selected row index.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *strCity = [arrCity objectAtIndex:indexPath.row];
[lblCity setText:strCity];
}
Issue: I only want to show the selected cells with checkmark. I don't want the grey highlight.
I tried:
cell.selectionStyle = UITableViewCellSelectionStyleNone
but didn't work.
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ProfileSelection *profile = [self.profileSelections objectAtIndex:indexPath.row];
cell.textLabel.text = [profile profileName];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.profileSelectionsTableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
ProfileSelection *profile = [self.profileSelections objectAtIndex:indexPath.row];
self.mobileProfileId = [profile.profileId stringValue];
[_continueButton setEnabled:YES];
}
How about this:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
The selectionStyle property should be enough to remove the highlight.
If you want to show the checkmark after the used has selected the row, you need to implement the tableView:didSelectRowAtIndexPath: method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView cellForRowAtIndexPath:visibleIndexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
In Swift, the easiest way is to deselect a cell without animation right after it has been selected:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
delete your this function
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Try to use this your problem will be solved
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone
cell.textLabel.text = #"Checking Table View";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Go to Storyboard. Select your Table View Cell. Set Selection to None. Then add below codes:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
This thread looks a bit old, but I actually came across a decent solution for this recently. In case anyone stumbles across this thread, I figure I might as well post an answer. Especially since I spent hours trying to track this down.
I can't find my original sources anymore, but here's what I found out:
The highlight is made by a background view on the UITableViewCell on the selectedBackgroundView property. If we have a subclass of the table cell, we can detect the transition to the editing state with willTransitionToState: and set the background view to one that is clear/white instead of the blue/gray styles.
Here's what I ended up doing, and it works pretty well.
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
// To remove the blue/gray highlight on the table cell when selected,
// set the selection background to a clear view.
if (state & UITableViewCellStateShowingEditControlMask) {
// If we're moving to an edit state, set the custom background view.
UIView *bgView = [[UIView alloc] init];
[bgView setTintColor:[UIColor whiteColor]];
[bgView setBackgroundColor:[UIColor clearColor]];
[self setSelectedBackgroundView:bgView];
} else {
// Otherwise, just remove it completely.
// The system should handle the rest.
[self setSelectedBackgroundView:nil];
}
}
The plus side to this over the other solutions I've seen is that this one is quite minimal. No need for tracking instance variables or anything else.
I know there are lot of answers to this question . But it didn't achieved the desired result . didDEselectrow is not working . I have a UIImageView which i have set to hidden True in cellforRowAtIndex. And when someone selects any row value for hidden is set to false .
**My main problem is when i select another row , the previous row status does not change and it also shows selected . I have attached the necessary snippet of my code ,Please check and help me through this . **
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
EventTypeTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Event_Type" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell.selectionStatus)
[cell.selectEvent setHidden:NO];
else
[cell.selectEvent setHidden:YES];
[cell.eventTypeName setText:[eventTypeName objectAtIndex:indexPath.row]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
EventTypeTableViewCell *cell = (EventTypeTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
if(cell.selectionStatus == TRUE)
{
cell.selectionStatus = FALSE;
}
else
{
] cell.selectionStatus = TRUE;
}
[tableView reloadData];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
EventTypeTableViewCell *cell = (EventTypeTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
if(cell.selectionStatus == FALSE)
{
//Do your stuff
cell.selectionStatus = TRUE;
}
else
{
//Do your stuff
cell.selectionStatus = FALSE;
}
[tableView reloadData];
}
I suspect that didDeselect is not being called because you are allowing multiple selections in your tableView.
Have you set your table view's selection style to "single selection"? You can either do it in interface builder/storyboard or you can do it programmatically:
- (void) viewDidLoad {
self.tableView.allowsMultipleSelection = NO;
}
That way when you select another row it will deselect the old selection. Also you can simplify your code and make everything look better by not calling [tableView reloadData] every time one cell's selected state changes. The way I would suggest doing that is to change the hidden state of selectEvent within EventTypeTableViewCell whenever the cell's selection changes.
That leaves you with this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
EventTypeTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Event_Type" forIndexPath:indexPath];
[cell.eventTypeName setText:[eventTypeName objectAtIndex:indexPath.row]];
return cell;
}
And some method overrides within your definition of EventTypeTableViewCell:
#implementation EventTypeTableViewCell
/// Your code ///
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
self.selectEvent.hidden = !selected;
}
#end
Try this ::
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
given below is my code to make image visible or not on multiple selection in uitableview but there is only 1 problem it doesnot work in realtime it works after scrolling.
i think i have mistake in picking cell of myTableView
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.selected)
{
cell.imageView.hidden = false;
}
else{
cell.imageView.hidden = true;
}
writing this code in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
,
this is the exect solution of your problem
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[TbView deselectRowAtIndexPath:indexPath animated:YES];
if (!([selectedIndexPath containsObject:indexPath]))
{
[selectedIndexPath addObject:indexPath];
[[TbView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[selectedIndexPath removeObject:indexPath];
[[TbView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
}
}
Better to use
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.hidden=!cell.imageView.hidden;
Try creating a subclass of UITableViewCell and overriding the setSelected method:
- (void)setSelected:(BOOL)selected
{
if (self.selected == selected)
return;
[super setSelected:selected];
if (selected)
{
self.imageView.hidden = NO;
}
else
{
self.imageView.hidden = YES;
}
}
P.S. don't forget to call setSelected on super as well.
Another option is use following two delegate method of UITableView.
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
// this method use for previous selected row/cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.hidden = false;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// this method use for selcted row/cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.hidden = true;
}
There is a much easier, simpler action than all this. Refresh table after selection has been made.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
//then reload table
[tableView reloadData];
}
So I have a subclassed UITableView that lists data. I need to make only one cell selectable.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// rows in section 0 should not be selectable
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
if (indexPath.row == 3) {
cell.userInteractionEnabled = YES;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
NSLog(#"Hey whats up, address");
return indexPath;
}
else {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
My code that does this so far works, but only after the cell is clicked at least once. Placing it in didSelectCell allows the cell to be selected if held down for 1-2 seconds.
You should do that in the cellForRowAtIndexPath. Like this:
-(UITableViewCell*)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// your code
if(indexPath.row != 3)
{
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
{
}
And then on didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == 3)
{
//do your stuff
{
}