This is something I don't understand. Look at this method (copied from http://blog.blackwhale.at/2009/07/uibutton-in-uitableview-footer/)
See that footerView is allocated on the beginning of the code and never released.
As far as I understand, the object should be autoreleased or at least released after used on a regular case, but as this method is run automatically by the tables when they are built, where exactly will this view be released...
or will it leak? I have seen Apple samples of code with stuff like that, so I suppose the object is being released somewhere... but where?
// custom view for footer. will be adjusted to default or specified footer height
// Notice: this will work only for one section within the table view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if(footerView == nil) {
//allocate the view if it doesn't exist yet
footerView = [[UIView alloc] init];
//we would like to show a gloosy red button, so get the image first
UIImage *image = [[UIImage imageNamed:#"button_red.png"]
stretchableImageWithLeftCapWidth:8 topCapHeight:8];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setBackgroundImage:image forState:UIControlStateNormal];
//the button should be as big as a table view cell
[button setFrame:CGRectMake(10, 3, 300, 44)];
//set title, font size and font color
[button setTitle:#"Remove" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//set action of the button
[button addTarget:self action:#selector(removeAction:)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
}
//return the view for the footer
return footerView;
}
thanks.
Seeing as footerView is an instance variable on this class, this code is correct. footerView is not autoreleased (by you, anyway) so it will continue to exist as long as you don't release it (because you "own"/retained it by allocating it). The proper place to do that would be in this class's dealloc method.
As long as you do that, this code looks correct to me. :)
footerView is an instance variable. Is it released in the dealloc of the class?
Related
I have this following code to add a button to my custom header for section.
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {
if (tableView == menuListTableView) {
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)] autorelease];
UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[headerButton setFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)];
[headerButton setImage:[UIImage imageNamed:#"Gray_Gradient.png"] forState:UIControlStateSelected];
[headerButton addTarget:nil action:#selector(toggleOpen:) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:headerButton];
return headerView;
}
return nil;
}
Why is my button not added to the custom header view?? The sub view array of the headerview seems to be nil when checked in the debugging mode.
Did you see the header view itself? Check the height of the header view. Also check whether the image exists. If in doubt, change the button type to rounded rect and see whether it was added properly.
Oh I got the answer myself!!! I looked at the button code.. and the control state was set to UIControlStateSelected. instead of UIControlStateNormal. Its so ignorant of me. Sorry guys for the bothering!!
I want to add a button in a UITableViewCell. This is my code: `
if (indexPath.row==2) {
UIButton *scanQRCodeButton = [[UIButton alloc]init];
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
scanQRCodeButton.backgroundColor = [UIColor redColor];
[scanQRCodeButton setTitle:#"Hello" forState:UIControlStateNormal];
[cell addSubview:scanQRCodeButton];
}`
Now, when I run the app, I see only a blank row ! Any ideas ?
While it's natural to put it in the contentView of the cell, I'm fairly certain that is not the problem (actually, in the past, I've never had subviews displayed correctly in the contentView, so I've always used the cell).
Anyway, the problem involves the first three lines of when you start creating your button. The first two lines are fine, but the code stops working with:
scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonWithType: is actually a convenience method to create a button (it's like a compact alloc-init). Therefore, it actually "nullifies" your past two lines (you basically created the button twice). You can only use either init or buttonWithType: for the same button, but not both.
UIButton *scanQRCodeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
scanQRCodeButton.frame = CGRectMake(0.0f, 5.0f, 320.0f, 44.0f);
scanQRCodeButton.backgroundColor = [UIColor redColor];
[scanQRCodeButton setTitle:#"Hello" forState:UIControlStateNormal];
[cell addSubview:scanQRCodeButton];
This will work (note that you can use cell.contentView if you wanted). In case you're not using Automatic Reference Counting (ARC), I would like to mention that you don't have to do anything in term of memory management, because buttonWithType: returns an autoreleased button.
UIButton *deletebtn=[[UIButton alloc]init];
deletebtn.frame=CGRectMake(50, 10, 20, 20);
deletebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[deletebtn setImage:[UIImage imageNamed:#"log_delete_touch.png"] forState:UIControlStateNormal];
[deletebtn addTarget:self action:#selector(DeleteRow:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deletebtn];
or
// Download class and import in your project UIButton+EventBlocks
UIButton *deletebtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[deletebtn setFrame:CGRectMake(170,5, 25, 25)];
deletebtn.tag=indexPath.row;
[deletebtn setImage:[UIImage imageNamed:#"log_delete_touch.png"] forState:UIControlStateNormal];
[deletebtn setOnTouchUpInside:^(id sender, UIEvent *event) {
//Your action here
}];
[cell addSubview:deletebtn];
You want to add any custom UI elements to the cell's contentView.
So, instead of [cell addSubview:scanQRCodeButton];
do [cell.contentView addSubview:scanQRCodeButton];
Try adding [cell.contentView addSubview:scanQRCodeButton]; or if you want the button to the left side look at my question at the answer, to move the textLabel to the side. If you want the button to the right then just set it as your accesoryView like this cell.accesoryView = scanQRCodeButton;.
Method setTitle does not work in my code, so I have set by using
[UIButton.titleLabel setText:#""]
instead of using setTitle method.
Please try again with following code:
[scanQRCodeButton.titleLabel setText:#"Hello"];
Then it would work well.
I have added uibutton in a cell in uitable view. I want this button to be pressed and interat with user. I added this line of code in cell implementation class.
readMore = [[UIButton alloc]initWithFrame: CGRectMake(180, 50, 67, 25)];
[readMore addTarget:self action:#selector(readMoreClic:) forControlEvents:UIControlEventTouchUpInside];
[readMore setImage:[UIImage imageNamed:#"readmore.png"] forState:UIControlStateNormal];
[self addSubview:readMore];
the problem is that it is not clicked (it is always in highlight state after selecting the cell). I tried :-
1- [self setSelectionStyle:UITableViewCellSelectionStyleNone];
2- cell.userInteractionEnabled = NO
but I still get cells selected and highlighted with blue color and button not clicked. even after i've chosen
[self setSelectionStyle:UITableViewCellSelectionStyleGray]
some one help plz
thx
[note i have a textView in the same cell and it allows interaction (select and copy) ]
[i am using xcode 4 with iOS 4.3]
try a different control event?:
[readMore addTarget:self action:#selector(readMoreClic:) forControlEvents:UIControlEventTouchDown];
well, i don't quite understand why your button don't respond, because i've came across similar problems but i've used different solutions. here's what i've done:
CGRect reuseableRect = CGRectMake(20, 290, 260, 37);
self.actionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.actionButton.frame = reuseableRect;
[self.actionButton setTitle:#"Initialize" forState:UIControlStateNormal];
[self.actionButton addTarget:nil action:#selector(performAction) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.actionButton];
Actually, i did this :
self.actionButton = [[UIButton alloc] initWithFrame:reuseableRect];
but it didn't work. so i changed it into
self.actionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
the resueableRect is just a CGRect that I've used all through the view initialization.
as for the three lines of your previous solution, the first one:
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
it's doing some configurations to the tableView and its cells, so it's not going to affect the button
the second one:
cell.userInteractionEnabled = NO;
it's not configuring the button as well, what's more, since the cell is the super view of the button, this line prevents any user interactions.
the third one is not doing anything to the button as well, so i recommend that you delete all of them if you were thinking about configuring the button
You don't need to do anything with selection style or userInteractionEnabled. Make sure that readMoreClic: looks like:
-(void)readMoreClic:(id)sender
{
}
I have written this code to see different image states...
UIButton *btnComment = [UIButton buttonWithType:UIButtonTypeCustom];
btnComment.tag=indexPath.row;
[btnComment addTarget:self action:#selector(goToComment:)forControlEvents:UIControlEventTouchDown];
UIImage *img1 = [UIImage imageNamed:#"commentbtndown.png"];
UIImage *img2 = [UIImage imageNamed:#"commentbtnup.png"];
UIImage *img3 = [UIImage imageNamed:#"commentbtnover.png"];
[btnComment setImage:img1 forState:UIControlStateNormal];
[btnComment setImage:img2 forState:UIControlStateHighlighted];
[btnComment setImage:img3 forState:UIControlStateSelected];
[btnComment setImage:img2 forState:(UIControlStateHighlighted+UIControlStateSelected)];
btnComment.frame =CGRectMake(0, 100, 95, 25);
[cell addSubview:btnComment];
[img1 release];
[img2 release];
[img3 release];
but its not working, it is always showing me image 1.
p.s. I have added these images in the table view cell
The problem is that you are creating the UIImage objects with an autorelease method imageNamed, and you are releasing these objects afterwards, which cause your button to have invalid objects and because of that the images will not be displayed
Try removing this lines of code and your button will work
[img1 release];
[img2 release];
[img3 release];
And also, if you want the button to receive the touch events, you will have to add it to the contentView of your cell object, otherwise the button will be shown but you will not be able to tap it.
[cell.contentView addSubview:btnComment]
Well one problem with your code is that you should not be releasing those image variables. imageNamed: returns an autoreleased UIImage. I'm doubt that this is causing your problem, though.
Try using | instead of + for your fourth setImage call.
Another problem with your code is where you add the button to your UITableViewCell. Instead of this:
[cell addSubview:btnComment];
You should add subviews to your cell's contentView:
[cell.contentView addSubview:btnComment];
But I'm also not certain that this may cause your problem...
this is driving me MAD now.. I have a UItableview. Based on an NSMutableArray, I populate it.
I set up in reuseTableViewCellWithIdentifier with the following
cellRectangle = CGRectMake((ARROW_OFFSET + 5), (ROW_HEIGHT - LABEL_HEIGHT) / 2.0, ARROW_WIDTH, LABEL_HEIGHT);
UIButton *tmpButton = [[UIButton alloc] initWithFrame:cellRectangle];
[tmpButton initWithFrame:cellRectangle];
[tmpButton setImage:[UIImage imageNamed:#"icon_edit.png"] forState:UIControlStateNormal];
[tmpButton setImage:[UIImage imageNamed:#"icon_no.png"] forState:UIControlStateDisabled];
[tmpButton addTarget:self action:#selector(editSelectedRow:) forControlEvents:UIControlEventTouchUpInside];
tmpButton.tag = ARROW_TAG;
[cell.contentView addSubview: tmpButton];
[tmpButton release];
then in cellForRowAtIndexPath I have the following lines of code
UIButton *button = (UIButton *)[cell viewWithTag:ARROW_TAG];
[button setTag:indexPath.row];
if (counterHasStarted == 1) {
NSLog(#"yes");
button.enabled = NO;
} else {
button.enabled = YES;
}
the button shows nicely, but for some reason when the counterHasStarted variable (which is an int is set, it doesn't change! I can change UILabels based on the above code (checking if counterHasChanged is 1 or 0).
Any ideas what's going on??
-cellForRowAtIndexPath: will only get called when the table view needs a new UITableViewCell because the user scrolled.
I guess you're changing counterHasStarted and expect the button enabled state to change? You could reload the data when you change counterHasStarted ([yourTableView reloadData]). Then the table view will call -cellForRowAtIndexPath: for all the cells that are currently visible, and you can enable or disable the buttons as needed.