I not find a solution to modify my custom cell since a button which is inside. I will wish to change the label text when I click the button.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
// View
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
[view setBackgroundColor:[UIColor colorWithWhite:255.0 alpha:0.0]];
// Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320-7-7, 16)];
[label setText:#"Hello"];
[label setTextAlignment:UITextAlignmentLeft];
[label setTextColor:[UIColor grayColor]];
[label setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:16]];
[label setBackgroundColor:[UIColor clearColor]];
[view addSubview:label];
// Action
UIButton *action = [[UIButton alloc] initWithFrame:CGRectMake(306-54, 0, 54, 54)];
[action setTitle:#"0" forState:UIControlStateNormal];
[action addTarget:self action:#selector(actionMore:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:action];
// Add view to cell
[cell addSubview:view];
return cell;
}
Edit (add details)
- (void)actionMore:(id)sender{
UIButton *button = (UIButton *)sender;
// Edit current cell, but I do not know how...
}
Thanks.
You can get the cell in which the button was pressed with following lines of code
UITableViewCell * cell = (UITableViewCell*)[[sender superview] superview];
Then use the cell reference to change the label text
cell.yourlabel.text = #"New Text";
Update
I think the above code may not work in iOS 7. The better way is
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.mainTable];
NSIndexPath *indexPath = [self.mainTable indexPathForRowAtPoint:buttonPosition];
Since your UIButton and UILabel are subviews of the same view, you can tag your label, and use viewWithTag to find it from your button's action code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320-7-7, 16)];
label.tag = 1234;
....
- (void)actionMore:(id)sender{
UIButton *button = (UIButton *)sender;
UILabel *label = [[sender superview] viewWithTag:1234];
// Edit current cell
}
To help you need to give some more details.
Is this a single cell or is it a prototype for multiple?
The code in the actionMore: selector.
What are you trying to do this for? Why a button in a cell to change the label text?
If this cell is being used more than once, a problem could be actionMore: can't isolate which cell is the target for the code. Also you could have a simple typo in the actionMore method but we can't solve that without being able to see all of the interacting code.
If you can provide more details, some one may be able to help. I will edit this answer if I can help you once I have more information.
--EDIT--
First to get cell you can use:
UITableViewCell * cell = (UITableViewCell*)[[sender superview] superview];
Then you can access your cells label by:
cell.label.text = #"Some Text Here";
Your next issue is figuring out where in the list your cell lies. For that use this code:
UITableView * table = (UITableView*)[[[sender superview] superview] superview];
NSIndexPath * indexPath = [table indexPathForCell: cell];
You then can then use a switch statement or if-else statements to find which row was triggered.
- (void)actionMore:(id)sender{
UITableViewCell * cell = (UITableViewCell*)[[sender superview] superview];
UIView *view = [cell.subviews objectAtIndex:0];
UILabel *label = [view.subviews objectAtIndex:0];
[label setText:#"test"];
}
you better add view as a subview of the contentview
pass the indexpath to actionMore or fetch it from there. once you got the indexpath, you are in business.
you may want to set a tag for your textlabel for fetching it from its superview.
Related
I have seen and checked multiple questions regarding adding an UIButton to a UITableViewCell but somehow I did not manage to find the exact problem that I am facing. When adding an UIButton to an UITableViewCell, the cell's text label overlaps with the button. The button is in the beginning of the row and it should be followed by the text label. I am most probably doing something wrong, but I can not find what. Here is my code for cellForRowAtIndex:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[self._tableView dequeueReusableCellWithIdentifier:#"event"];
Event* event =[self eventForIndexPath:indexPath];
if(cell==nil)
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"event"];
[[cell textLabel] setText:event.customer.name];
if(event.startTime!=nil)
[[cell detailTextLabel] setText:event.startTime];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
UIButton *cellButton =(UIButton*) [cell.contentView viewWithTag:CELL_BUTTON_TAG];
if(cellButton == nil){
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.tag=CELL_BUTTON_TAG;
[cellButton setFrame:CGRectMake(0.0f, 0.0f, 44.0f, cell.frame.size.height)];
[cellButton setImage:[UIImage imageNamed:#"van_green.png"] forState:UIControlStateNormal];
[cellButton setImage:[UIImage imageNamed:#"location.png"]forState:UIControlStateSelected];
[cellButton addTarget:self action:#selector(carButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
cellButton.layer.borderWidth = 0.0;
[cell.contentView addSubview:cellButton];
}
return cell;
}
Sorry but your code is completely wrong..you are using an incorrect way and also you are adding a button each time.
The tableViewCell are REUSED so you have to add buttons and other UI elements just when you INIT the cell and not each time, or your app will finish in memory warning, crash, not fluid etc.
The correct way to do what you want to do is create your CustomTableViewCell subclassing the class UITableViewCell and add your labels and buttons. It is really simple:
1) Create a new file CustomSearchCell object that extends UITableViewCell:
CustomSearchCell.h
#import <UIKit/UIKit.h>
#interface CustomSearchCell : UITableViewCell
#property (strong, nonatomic) UIButton *button;
#property (strong, nonatomic) UILabel *lblDetails;
#end
CustomSearchCell.m
#import "CustomSearchCell.h"
#implementation CustomSearchCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
_lblDetails = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 200, 45)];
[_lblDetails setFont:[UIFont systemFontOfSize:20.0]];
[_lblDetails setTextColor:[UIColor blackColor]];
_button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, self.frame.size.height)];
[self.contentView addSubview: _lblDetails];
[self.contentView addSubview: _button];
}
return self;
}
#end
2) In your view controller:
#import "CustomSearchCell.h"
and:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListCellIdentifier";
CustomSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[CustomSearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Settings
return cell;
}
Another your problem is when you press the button. You will receive the sender but the tableViewCell will can be used for a different indexPath when you intercept the touch..be careful..the correct way is another..you need to save in some way the actual indexPath when you press the button...
Problem is in this line [cell.contentView addSubview:cellButton];. When your cell going to be reuse, the cell already contains cell button, and you try to add that button again. Just do the below step to resolve this issue.
1) Add button tag to take from base view.
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.tag = CELLBUTTONTAG;
2) Check condition view tag and add it to base view as below.
UIButton *cellButton = [cell.contentView viewWithTag:CELLBUTTONTAG];
if(cellButton == nil)
{
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
cellButton.tag = CELLBUTTONTAG;
...
[cell.contentView addSubview:cellButton];
}
You have given cellbutton's y position as 0.0f.That's y overlapping. try to change some value as you need.
[cellButton setFrame:CGRectMake(0.0f, 0.0f, 44.0f, cell.frame.size.height)];
to
[cellButton setFrame:CGRectMake(0.0f, some big value, 44.0f, cell.frame.size.height)];
I have a subview within UITableViewCell's that contains a UILabel and a UIButton. When I push the button it unselects the UITableViewCell for some reason.
Is there a way to pass the touch event through to its super view? Or is there a different problem all together?
Here is the code for CellForRowAtIndexPath. I plan on creating a custom subclass of UITableViewCell so I can handle cell reuse properly so don't feel the need to mention that.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CountModel *cellModel = self.viewModel.data[indexPath.row];
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"RootViewControllerMetricCell"];
cell.textLabel.text = cellModel.title;
// Custom background color for selection
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = UIColorFromRGB(0xF3F3F3);
bgColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView:bgColorView];
// Construct subview and append to bottom (only visible when selected)
UIView *cellSubView = [[UIView alloc] initWithFrame:CGRectMake(0, 125, cell.frame.size.width, 25)];
// UILabel for display of current count
UILabel *lblCount = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 25, 25)];
lblCount.text = [NSString stringWithFormat:#"%d", [cellModel.count intValue]];
// Create UIButton for incrementing
UIButton *incrementButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
incrementButton.frame = CGRectMake(45, 0, 25, 25);
[incrementButton setTitle:#"+" forState:UIControlStateNormal];
// Enable touch event for button view
[incrementButton addTarget:self action:#selector(countButtonTouchDown:) forControlEvents:UIControlEventTouchDown];
[cellSubView addSubview:lblCount];
[cellSubView addSubview:incrementButton];
[cell addSubview:cellSubView];
return cell;
}
How about you add the [self.tableView cellForRowAtIndePath:] setSelected:] inside the function responding to the button press to reinstate the same selected/ deselected state.
UPDATE:
Shift the code inside the didSelectCellAtIndexPath to another function, say performActionOnSelectionOfCellAtIndexPath and call performActionOnSelectionOfCellAtIndexPath from inside the didSelectCellAtIndexPath with the indexPath and also call performActionOnSelectionOfCellAtIndexPath from the function responding to the button press.
I use custom cell on UITableView using QuickDialog
In this custom cell I have several UILabel, UIImageView and a button which has the cell size and display on the top of the others subviews.
I want this button handle touch envent and call a selector. But even though the button is at the top position, the touch event does not trigger when I touch a subview.
- (UITableViewCell *)getCellForTableView:(QuickDialogTableView *)tableView controller:(QuickDialogController *)controller
{
UITableViewCell *cell = [super getCellForTableView:tableView controller:controller];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = cell.frame;
[btn setEnabled:YES];
[btn setExclusiveTouch:YES];
[btn setBackgroundColor:[UIColor blueColor]]; // To check if the button is at the top position
[btn setStringTag:labelIdText];
[btn addTarget:self action:#selector(handleTap:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:label1];
[cell addSubview:label2];
[cell addSubview:image1];
[cell addSubview:image2];
[cell addSubview:btn];
cell.userInteractionEnabled = YES;
return cell;
}
The selector :
- (IBAction)handleTap:(id)sender
{
NSLog(#"CELL TAPPED : \n");
}
Thanks a lot.
EDIT :
New version of the code :
- (UITableViewCell *)getCellForTableView:(QuickDialogTableView *)tableView controller:(QuickDialogController *)controller
{
UITableViewCell *cell = [super getCellForTableView:tableView controller:controller];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[top setStringTag:labelIdText];
[top addGestureRecognizer:singleFingerTap];
[cell.contentView addSubview:label_1];
[cell.contentView addSubview:label_2];
[cell.contentView addSubview:image_1];
[cell.contentView addSubview:image_2];
[cell.contentView addSubview:top];
cell.userInteractionEnabled = YES;
image_1.userInteractionEnabled = YES;
image_2.userInteractionEnabled = YES;
return cell;
}
The selector :
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
NSLog(#"CELL TAPPED : \n");
}
The event is handle when I touch the text containing in the UILabel.
But it's still don't work with the UIImageView despite userInteractionEnabled setted to YES for the images.
Thanks again.
It seams you are assigning to the button the cell frame and then add it to the cell itself. In this way the position of the button is not the one you expect, and even though you can see the button it will not respond to touch because it resides outside the cell bounds.
Try to change this:
btn.frame = cell.frame;
to this:
btn.frame = cell.bounds;
Also when working with UItableViewCell remember to add subviews to its contentView and not the cell itself:
[cell.contentView addSubview:aCustomView];
I have a UITableView with 20 rows. In each row, two UIButtons are added, so totally I have 40 buttons. How can I access each button in each cell? All those UIButton have the two tags 1 and 2.
For example: I want to write a method to change the background color of 2 buttons in a particular row:
-(void)changeColor:(int)row{
//code here
}
Do you have any suggestion?
-(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"Cell"] autorelease];
UIButton *button1=[[UIButton alloc] initWithFrame:CGRectMake(x1,y1,w1,h1)];
UIButton *button2=[[UIButton alloc] initWithFrame:CGRectMake(x2,y2,w2,h2)];
[button1 setTag:1];
[button2 setTag:2];
[[cell contentView] addSubview:button0 ];
[[cell contentView] addSubview:button1 ];
[button0 release];
[button1 release];
}
UIButton *button1 = (UIButton *)[cell viewWithTag:1];
UIButton *button2 = (UIButton *)[cell viewWithTag:2];
return cell;
}
You should be able to do this:
-changeColor:(int)row
{
NSIndexPath indexPath = [NSIndexPath indexPathForRow:row inSection:0]; // Assuming one section
UITableViewCell *cell = [myTableView cellForRowAtIndexPath:indexPath];
UIButton *button1 = (UIButton *)[[cell contentView] viewWithTag:1];
UIButton *button2 = (UIButton *)[[cell contentView] viewWithTag:2];
}
*some syntax might be wrong. I dont have Xcode handy
If you want to change the button color to have a highlighted / downstate I suggest to use the following:
[yourButton addTarget:self action:#selector(goToView:) forControlEvents:UIControlEventTouchUpInside];
[yourButton addTarget:self action:#selector(changeColor:) forControlEvents:UIControlEventTouchDown];
[yourButton addTarget:self action:#selector(touchCancel:) forControlEvents:UIControlEventTouchDragExit];
-(void)buttonAction:(UIButton*)sender
{
[self touchCancel:sender];
/* DO SOME MORE ACTIONS */
}
-(void)changeColor:(UIButton*)sender
{
sender.backgroundColor = [UIColor redColor];
}
-(void)touchCancel:(UIButton*)sender
{
sender.backgroundColor = [UIColor clearColor];
}
If you want to make the button have a different selected color, create an array that holds all the selected states. Then use the array to check if the button color needs a different color in cellForRowAtIndexPath.
When you want to change the button color just change the value in the array and call
[self.tableView reloadData];
Be sure to do it outside the if (cell == nil) method so it is also called when the cell is reused.
I'm trying to add subviews to a view according to the cell of the tableview .. i've done this code already
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VerseCell *cell = (VerseCell *)[tableView cellForRowAtIndexPath:indexPath];
rowSelected = indexPath.row;
CGRect cellRect = cell.frame;
UIView *cellView =[[UIView alloc]initWithFrame:CGRectMake(cellRect.origin.x+4, cellRect.origin.y+15, cellRect.size.width-20, 70)];
UIImageView* background =[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"FavoritePanelBackground"]];
background.frame =CGRectMake(cellRect.origin.x+4, cellRect.origin.y+15, cellRect.size.width-20, 70);
[cellView addSubview:background];
[cellView setBackgroundColor:[UIColor redColor] ];
UIButton * favoritsB = [[UIButton alloc]initWithFrame:CGRectMake(cellRect.origin.x+10, cellRect.origin.y+15, 60, 40)];
[favoritsB setBackgroundImage:[UIImage imageNamed:#"MofadalatNormalIcon.png"] forState:UIControlStateNormal];
[favoritsB setBackgroundImage:[UIImage imageNamed:#"MofadalatTappedIcon.png"] forState:UIControlStateHighlighted];
[favoritsB addTarget:self action:#selector(favorite) forControlEvents:UIControlEventTouchUpInside];
[cellView addSubview:favoritsB];
cellView=nil;
}
the problem is .. that this code work as expected in the first row only.. and when click on next cell it shows the main UIVIew (which i've mark with red background) in the expected position while the sub views of it is going really far down... so what i'm doing wrong here.. and is there another approach for what i'm doing if that will not work?
Where are you trying to add these view? Inside the table view cell? If so, you never add cellView as a subview of cell. IF that's what you're trying to do, add this line just above cellView = nil:
[cell addSubview:cellView];