MLKMenuPopover in UITableViewCell not working - ios

I am using MLKMenuPopover in UITableViewCell not working and it's not showing correctly and also not working
There is line on the menu of cell separator
This is the code for creating the round button for popup in custom UITableViewCell
_btnDelete=[UIButton buttonWithType:UIButtonTypeCustom];
[_btnDelete setFrame:CGRectMake(DeviceWidth-30, 80, 20, 20)];
[_btnDelete setBackgroundImage:[UIImage imageNamed:#"edit_icon.png"] forState:UIControlStateNormal];
[_btnDelete addTarget:self action:#selector(btnRemoveClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:_btnDelete];
And this action is also custom UITableViewCell
-(void)btnRemoveClicked:(id)sender{
if (self.delegate!=nil && [self.delegate respondsToSelector:#selector(delegatebtnAddressRemoveClicked:)]) {
[self.delegate delegatebtnAddressRemoveClicked:sender];
}
}
And here is the delegate method
-(void)delegatebtnRemoveClicked:(id)sender{
self.menuItems = [NSArray arrayWithObjects:#"REMOVE", nil];
NSLog(#"Button remove Clicked");
[self.menuPopover dismissMenuPopover];
self.menuPopover = [[MLKMenuPopover alloc] initWithFrame:MENU_POPOVER_FRAME menuItems:self.menuItems];
self.menuPopover.menuPopoverDelegate = self;
[self.menuPopover showInView:sender];
}

Related

Switching UISwitch on/off by pressing on UILabel

I've got this method were it displays UILabels and UISwitches right next to them. I want the UISwitch to be switched on and off by pressing on a label as well. I have tagged the switches but I don't know what to do next.. Any help would much appreciated. Thanks!
while (i < numberOfAnswers) {
UILabel *answerLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, y+spaceBetweenAnswers, 240, 30)];
answerLabel.text = [NSString stringWithFormat:#"%# (%#)", questionBank[randomQuestionNumber][1][i][0],questionBank[randomQuestionNumber][1][i][1]];
answerLabel.font = [UIFont systemFontOfSize:15];
answerLabel.textColor = [UIColor darkGrayColor];
answerLabel.numberOfLines=0;
[answerLabel sizeToFit];
[_answerView addSubview:answerLabel];
answerHeight = answerLabel.frame.size.height + spaceBetweenAnswers;
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(10, y+spaceBetweenAnswers-5, 0, 30)];
mySwitch.transform = CGAffineTransformMakeScale(0.75, 0.75);
if ([questionBank[randomQuestionNumber][1][i][1] isEqual:#"0"]) {
[mySwitch addTarget:self action:#selector(wrongAnswer:) forControlEvents:UIControlEventValueChanged];
} else {
}
mySwitch.tag = i;
[_answerView addSubview:mySwitch];
}
Use a UIButton instead of a UILabel and set an IBAction on it. You can style the button to look exactly like the label. Your IBAction method should look something like this:
- (void)buttonPressed:(UIButton *)sender
{
//Get the view by tag
UISwitch *mySwitch = (UISwitch *)[self.view viewWithTag:yourTag];
[mySwitch.setOn:![mySwitch isOn]];
}
Edit
As mentioned, since you're building the UIButtons in code, you need to add the target to the button to get the button click. Add the action as such:
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Add a tap gesture recognizer to each label. When it is tapped, you can get the label from the gesture (view). Now you just need to get the switch to update.
You could add a tag to the label which is 1000 + i, then a simple subtraction will give you the switch tag that you need to find.
You could tag the labels and use the tag as the index into an array or switches (possibly IBOutletCollection).
You can do something like this.
UISwitch* mySwitch = [[UISwitch alloc]init];
[self addSubview:mySwitch];
UIButton* switchLabelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
switchLabelBtn.frame = CGRectMake(0, 0, 80, 40);
[switchLabelBtn addTarget:self action:#selector(switchLabelbtnTapped:) forControlEvents:UIControlEventTouchUpInside];
[switchLabelBtn.layer setValue:mySwitch forKey:#"Switch"];
[self addSubview:switchLabelBtn];
- (void)switchLabelbtnTapped:(UIButton*)sender
{
UISwitch* mySwitch = [sender.layer valueForKey:#"Switch"];
mySwitch.on = ![mySwitch isOn];
}

How to set the position of ImageView after getting x and y postion on finger touch in iphone

I am making an ipad app in which I have tableView with custom it works fine but I want that when user clicks on add button in custom cell then it should get the position x and y of that button and then set the imageView to the center of that button.
I have searced code it gets points but how to add make this for cell.addButton as cell is custom.
[cell.imageButton addTarget:self action:#selector(imageButtonActionOne:) forControlEvents:UIControlEventTouchUpInside];
[cell.addButton addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
Here is the code for touch
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];
But in this way it does not call the method which is on cell.addButton imageButtonAction.
As I understand from you , you need to set an image in a UIButton upon tap.
If you added the UITapGestureRecognizer you removed the default tap recognition and now only this would work:
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];
You should remove the above code and only set this:
[cell.addButton addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
where the imageButtonAction: is :
- (void )imageButtonAction:(UIButton *) b
{
[b setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
}
If you want to add an image next to the button let's say in the left of the button then the function should look like this:
- (void )imageButtonAction:(UIButton *) b
{
UITableViewCell *cell = (UITableViewCell*)[b superview];
NSInteger imgWidth = 50;
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(b.frame.origin.x - imgWidth,b.frame.origin.y,imgWidth,b.frame.size.height)];
img.backgroundColor = [UIColor redColor];
[cell addSubview:img];
[img release];
}
UIButton *btn = (UIButton *)sender;
UIImageView *backimage = [[UIImageView alloc] initWithFrame:CGRectMake(10,0,312,105)];
//If you want to do this set interaction enabled.
backimage.userInteractionEnabled = YES;
backimage.image=[UIImage imageNamed:#"popupj.png"];
tab1 = [UIButton buttonWithType:UIButtonTypeCustom];
[tab1 setFrame:CGRectMake(-1,2,293,58)];
[tab1 setTitle:#"Record Video" forState:UIControlStateNormal];
[tab1 addTarget:self action:#selector(tab1Action) forControlEvents:UIControlEventTouchUpInside];
[tab1 setImage:[UIImage imageNamed:#"popuptab1j.png"] forState:UIControlStateNormal];
[backimage addSubview:tab1];
now i am adding button to imageView but it does not get clicked
For UIButton you can use directly button function
-(IBAction) imageButtonAction:(id) sender
{
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
UIButton *btn = (UIButton *)sender;
//[btn setimage:[UIImage imagenamed:dimagename];
UIImageview *backimage = [[UIImageView alloc] initWithFrame:btn.frame];
[cell addSubview:backimage];
}

how to stop custom UITableViewCell covering UITableView border lines

Perhaps this is a very simple answer, but I have searched everywhere and it seems everyone is trying to do the opposite of what I am trying to do. I have a custom UITableViewCell as defined below.
#import "TagCell.h"
#import "TagRepository.h"
#import "TagsViewController.h"
#implementation TagCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self setFrame:CGRectMake(0, 1, self.frame.size.width, 37)];
self.selectionStyle= UITableViewCellSelectionStyleNone;
self.textLabel.hidden=NO;
[self.textLabel setFrame:CGRectMake(self.bounds.size.width/2-40, self.bounds.origin.y+1, 300, 30)];
self.textLabel.text= #"test";
[self.textLabel setFont:[UIFont fontWithName:#"Helvetics" size:30] ];
}
return self;
}
-(void) layoutSubviews
{
NSArray *viewsToRemove = [self subviews];
for (UIView *v in viewsToRemove) {
if([v isKindOfClass:[UIButton class]])
{
[v removeFromSuperview]; //used to remove and redraw the button during a orientation change
}
}
UIButton * removeButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[removeButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:18] ];
[removeButton setFrame:CGRectMake(7*self.bounds.size.width/8, self.bounds.origin.y+3, 30, 30)];
removeButton.hidden=NO;
[removeButton addTarget:self action:#selector(removeTag) forControlEvents:UIControlEventTouchUpInside];
[removeButton addTarget:self action:#selector(removeTag) forControlEvents:UIControlEventTouchDragInside];
[removeButton setBackgroundImage:[UIImage imageNamed:#"button_red_unselect.png"] forState:UIControlStateNormal];
[removeButton setBackgroundImage:[UIImage imageNamed:#"button_red_select.png"] forState:UIControlStateSelected];
[removeButton setImage:[UIImage imageNamed:#"icon_minus.png"] forState:UIControlStateNormal];
[removeButton setImage:[UIImage imageNamed:#"icon_minus.png"] forState:UIControlStateSelected];
[self addSubview:removeButton];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void) removeTag
{
for( int i=0 ; i< [TagRepository instance].tags.count; i++)
{
if([self.textLabel.text isEqualToString:[TagRepository instance].tags[i]])
{
[[TagRepository instance].tags removeObjectAtIndex:i] ;
}
}
_reloadTable();
// [_delegate.table reloadData];
//RELOAD TABLE
}
#end
So what happens is, that when I open up the tableview when there is an active custom cell the border is not present and it appears as if there is a blank cell following it.. or something.
This is not desired I wish the cell had a border surrounding it.
The view that contains the table sets heightforcellatindex:indexpath = 40, so that delegate function returns 40 as the cell height value, I assume that the problem lies between some sort of mismatch between this and the subviews in the custom cell. Do you see anything wrong with this??
note that I have already changed the typo in Helvetica.
If you add [super layoutSubviews] to your layoutSubviews method, the cell separators will appear. I'm not sure what you mean by the blank cell following it -- I didn't see anything like that when I tried your custom cell. Also, you should be adding (and removing) your buttons to the cell's contentView, not to the cell itself.

Table view button index cell

I have one table view in that cell is custom. I am adding two button on each cell of table view. when I click on first button at same time second button from same cell is changing its image. for that I have methods as editQuantity and Cancelorder. using #sel. I am getting an issue that when i click on first button insted of changing same cell another button its changing another cells button also when I scroll table view its losses all selected button
Here Is My Code--
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
simpleTableIdentifier = #"MenuNameCell";
MenuNameCell *cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell== nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuNameCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog(#"---------new cell agin");
}
else
{
NSArray *arrayView = [cell.contentView subviews];
for (UIView *vTemp in arrayView)
{
[vTemp removeFromSuperview];
}
NSLog(#"---No New Cell hiiii");
// Setting Tag To UIButton
_checkButton = (UIButton *)[cell.contentView viewWithTag:indexPath.row];
_cancelButton = (UIButton *)[cell.contentView viewWithTag:indexPath.row];
}
// Creating Label Menu Name
_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 11, 82, 21)];
_nameLabel.backgroundColor = [UIColor clearColor];
_nameLabel.text = [_hotel._orderedMenus objectAtIndex:indexPath.row];
// Creating Label Menu Cost
_amountMenu = [[UILabel alloc] initWithFrame:CGRectMake(167, 13, 44, 21)];
_amountMenu.backgroundColor = [UIColor clearColor];
_amountMenu.text = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];
// Creating Text Field For Order Quantity
_textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
_textFieldQuantity.userInteractionEnabled = NO;
_textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];
// Creating Button For Check Order
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
[_checkButton setTag:indexPath.row];
_checkButton.titleLabel.tag = indexPath.row;
[_checkButton setBackgroundImage:[UIImage imageNamed:#"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
// Creating Button For CANCEL Order
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
[_cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[_cancelButton setTag:indexPath.row];
_cancelButton.titleLabel.tag = indexPath.row;
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
// Adding All To Content View
[cell.contentView addSubview:_nameLabel];
[cell.contentView addSubview:_amountMenu];
[cell.contentView addSubview:_textFieldQuantity];
[cell.contentView addSubview:_checkButton];
[cell.contentView addSubview:_cancelButton];
//objc_setAssociatedObject(_checkButton, iindex, indexPath,OBJC_ASSOCIATION_RETAIN );
return cell;
}
-(void)editQuantity:(id)sender{
button = (UIButton *)sender;
row = button.tag;
col = button.titleLabel.tag;
NSLog(#"Check Button index is %d",row);
NSLog(#"cehck title is %d",col);
_textFieldQuantity.userInteractionEnabled = YES;
UIImage *buttonImage = [UIImage imageNamed:#"edit_over.png"];
[_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
UIImage *buttonImageCancel = [UIImage imageNamed:#"check.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
_cancelButton.tag = 0;
}
-(void)cancelOreder:(id)sender{
button = (UIButton *)sender;
row = button.tag;
NSLog(#"The Row Selected iS At Cancel Order ISSSS----%d", row);
if (_cancelButton.tag == 0){
_textFieldQuantity.userInteractionEnabled = NO;
UIImage *buttonImageCancel = [UIImage imageNamed:#"check_over.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
UIImage *buttonImageCancel1 = [UIImage imageNamed:#"cancel.png"];
[_cancelButton setBackgroundImage:buttonImageCancel1 forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"edit.png"];
[_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
_cancelButton.tag = 1;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iHomeDelivery" message:#"Do You Want To Cancel the Order" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
The problem is that the default tag value for any control is set to 0 and you have used the _cancelButton.tag = 0;. Just change the value to 10 and then _cancelButton.tag = 1; change 1 to 11.
This will solve your problem :)
As I see _checkButton, _nameLabel, _amountMenu, _textFieldQuantity & _cancelButton all are instance variable of your view controller class.
In cellForRowAtIndexPath: as you are assigning new objects to all these, they will refere to objects of last cell displayed. Means all these will point to components from last cell.
Hence changing background images for _checkButton & _cancelButton will affect for buttons in last cell.
Also in cellForRowAtIndexPath: while creating these buttons, you are setting background image for them, hence after scrolling your button images are getting changed. Remember cellForRowAtIndexPath: gets called for a row each time that row becomes visible after getting hidden while scrolling.
You need to maintain state of buttons in separate array, so that you can reset state for buttons in cellForRowAtIndexPath: after dequeuing.

the event touchUpInside of a UIButton don't work

there is only 1 cell in my UITableView, and I add a UIButton to the cell.
set the action when touchUpInside event happens. But it doesn't work at all. Meanwhile, if
i set the trigger as touchDown event instead of touchUpInside, it will fire the action i set.
I search the reason by google, someone says that the problem maybe caused by the superview's dimension. That is the size of the button is beyond its superview.
so I add a UIView to contain the button, the code is as follow:
self.ageDisplay = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
UIImage *ageImage = [UIImage imageNamed:#"long_btn_click.png"];
[self.ageDisplay setImage:ageImage forState:UIControlStateSelected];
[self.ageDisplay setFrame:CGRectMake(10, 5, 145, 34)];//(10, 320, 145, 25)];
[self.ageDisplay setTitle:#"请选择您的年龄" forState:UIControlStateNormal];
[self.ageDisplay setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.ageDisplay setTag:3];
[self.ageDisplay addTarget:self action:#selector(showSelectionPage:) forControlEvents:UIControlEventTouchDown];
self.buttonsSubView = [[UIView alloc] initWithFrame:CGRectMake(0, 310, 320, 106)];
//self.buttonsSubView.backgroundColor = [UIColor blueColor];
[self.buttonsSubView setUserInteractionEnabled:YES];
[self.buttonsSubView addSubview:self.ageDisplay];
the above code is in ViewDidLoad function.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *sugPageCell = #"SuggestionPageCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:sugPageCell];
if (nil == cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sugPageCell];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell addSubview:self.sugTitle];
[cell addSubview:self.sugSpecification];
[cell addSubview:self.suggestion];
self.buttonsSubView.userInteractionEnabled = YES;
[cell addSubview:self.buttonsSubView];
/*
[cell addSubview:self.ageDisplay];
[cell addSubview:self.genderDisplay];
[cell addSubview:self.submit];
*/
return cell;
}
However, this doesn't help.
The problem has confused me for 2 days.
I will appreciate if anyone can provide some indication.
Thank you.
It's because your buttonSubView view is outside the cell's frame.
So even if the button is visible (because the cell's clipsToBounds is set to NO), it will not receive touch events from the cell. Why do you set this view's vertical position to 310 ?
If you try this :
self.buttonsSubView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 106)];
The event should fire as you want.
I had this problem recently. I guess there must may be an object of UITapGesture added to your UITableView. If so, please set the property cancelsTouchesInView of the UITapGesture object to NO.
For example:
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:tapAction];
gesture.cancelsTouchesInView = NO;
[self addGestureRecognizer:gesture];

Resources