I have no idea how to do this. I have a tableview on firstview with some labels. And they have a disclosure button I want to obtain the data from the labels and show them in secondview in other labels.
How to do it please?
Thank you
To get the selected cell you have to implement:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
YourCellType *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
}
YourCellType is an UITableViewCell subclass having the desired parameters (UILabel, UIImageView, etc.).
So if you have a UILabel *label1 in cell, you can access it by:
cell.label1
Hope this helps
Related
When we select UITableViewCell than - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath called.
But which method will be called if we hold the UITebleViewCell. Guys my problem is that I have created a tableview which contain large cell and in that cell I am setting various view an view's background color.
When I select the cell, the background color of my cell will be gone. I have solved this problem by setting view background color again in didSelectRowAtIndexPath method like this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
UIView *vLineview=[selectedCell viewWithTag:1];
vLineview.backgroundColor = [UIColor colorWithRed:(89/255.0) green:(89/255.0) blue:(89/255.0) alpha:1];
}
This done the trick and my view background color will displayed but when I hold the UITableViewCell than it will gone again.
How can I solve this? Do I have to and gesture recognizer to detect long touch and implement my view background method in it? Or there is any other method available for that.
Try with set cell selection style none like this in cellForRowAtIndexPath
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
You can subclass UITableViewCell and override this method:
- (void) setSelected: (BOOL) selected
animated: (BOOL) animated
{
[super setSelected: selected
animated: animated];
// Configure the view for the selected state
}
Is this an issue with the UITableViewCell remaining selected after you tap on the row?
If row selection is not required, make sure you call deselectRowAtIndexPath:animated at the end of your tableView:didSelectRowAtIndexPath method.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Pleasese see method named -(void)beginUpdates
Call this method if you want subsequent insertions, deletion, and selection operations (for example, cellForRowAtIndexPath: and indexPathsForVisibleRows) to be animated simultaneously.
for more detail visit apple documentation in below link
UITableView Class Refrence
Also try [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
I have a table view with a list of items. When I click on one of those items I want the background to be highlighted. I have that code working but the colour changes on release, not on the click itself. How can I highlight when the user taps on the cell and not when he/she releases it?
Here is my code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// Navigation logic may go here. Create and push another view controller.
Help_Cell *cell =(Help_Cell*) [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor=[self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor2"]];
cell.backgroundView=v;
cell.title.textColor=[self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor1"]];
}
I want it to happen like a button. Is there an onClick method for a UITableView or a UITableViewCell besides didSelectRowAtIndexPath?
EDIT
Here is my code from cellForRowAtIndexPath
-(void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Help_Cell *cell =(Help_Cell*) [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor=[self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor2"]];
cell.selectedBackgroundView=v;
}
At the time of creating cell, in cellForRowAtIndexPath, write below code:
UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor = [self colorForHex:[appDel.appColorSettings objectForKey:#"cellColor2"]];
cell.selectedBackgroundView = v;
No need to write anything in didSelectRow method.
You can consider this as option
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView selectRowAtIndexPath:indexPath.row animated:YES scrollPosition:UITableViewScrollPositionTop];
}
I am new to the iPhone development. I got stuck with a problem. I want a check box function implemented in my UITableView. But my UITableViewCells are custom cell which consist of image and 3 UILabels. I could bring a sub view to the table view so that check box image can placed and I am successful in that. But the problem comes is whenever I click on the image only the last cell check box get changed. I would like to access the cell which I've clicked.
I have tried this
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
But this crashes since cell is custom.
Can any one suggest me a good method to do that?
Instead of using
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
try using
YourCustomCell *cell = (YourCustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
Hope it helps
First of all you need to define a tag -
#pragma imageViewTag 1
then in your cellForRowAtIndexPath assign this tag to your image view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
then you can access your image view any where with the help of this tag as -
UITableViewCell *cellView = (UITableViewCell*) [tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
UIImageView *imgView = (UIImageView*) [cellView viewWithTag:imageViewTag];
In your TableView DidSelect Delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *image = [cell.contentView viewWithTag:15];
//You should have set the tag value as 15 when creating the image in cell
//and you should have added to the cell contentview
}
If you cant able to get the cell then probably you wouldnt have used the reusable cell concept correctly. So post your entire cellforrowindex code.
If you are using the check kind of thing. Why dont you use the default disclosure indicator of tableview instead of your image.
I have a UITableView and have it so when you press the first cell, it takes you to a new view (new xib). When you press the second cell, you go to another view and so on. When you go to any of those new views, and come back to the tableview view, the cell you just pressed is still selected (its highlighted blue). What is the code to fix this?
In your tableView datasource delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Inside viewWillAppear of your tableView whose cell you wish to remove selection from:
UITableViewCell *cell = (UITableViewCell *)[myTableView cellForRowAtIndexPath:lastSelected];
[cell setSelected:NO];
Where lastSelected can be a global var of type NSIndexPath storing indexPath from the didSelectRowAtIndexPath of the above UITableView
It's quite easy..
In the same method that you push the new view you should add the line:
[tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated];
The cell in this iPhone TableView definitely has a Detail-Disclosure-Button.
When I tap it... shouldn't this code give me access to the button?
Instead detailButton is always just null.
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *detailButton = (UIButton *)[aCell accessoryView];
}
are you over-riding the cell accessory button in 'cellForRowAtIndexPath' by any chance?
accessoryView is provided for your own customization, to override accessoryType. The table view cell will internally handle whatever you assign to accessoryType without affecting accessoryView.