dynamically recreate imageviews, labels and buttons - iphone

I started working on an iphone app and since I am not as experienced as most I can not figure out how to recreate dynamically imageviews, labels and buttons. Basically there will be an array in which some data is stored. However the size of the array will vary and such I can not make a static VC on the storyboard. I would like to use the following template and then the array should use this template to recreate it as many times as it needs :
http://www.dropinsight.eu/screenshot1.png
Thank you in advance for any help you can give me.
Cheers
Max

You could consider putting your imageviews, labels and buttons in a UITableView.
Rather than use a UIViewController in your storyboard, use a UITableViewController. This will be the datasource and delegate of the tableView, and can be used to create UITableViewCells. Each cell could contain an imageView, text and 2 buttons per your example
See Apple docs for more details.

The previous answers about using a UITableView to display each entry in the array as a cell is the way you want to go. To answer your question about "dynamically creating" each item, you would want to create each UI element in your cellForRowAtIndexPath like this:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,w,h)];
[imageView setTag:1];
[cell addSubview:imageView];
[imageView release];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x,y,w,h)];
[label setFont:[UIFont systemFontOfSize:X]];
[label setTag:2];
[cell addSubview:label];
[label release];
UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(x,y,w,h)];
[button1 setImage:[UIImage imageNamed:#"ABC"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
[button1 setTag:3];
[cell addSubview:button1];
[button1 release];
etc
Then in your willDisplayCell method you will set the contents of any UI item that change for each row like this:
UIImageView *imageView = (UIIamgeView *)[cell viewWithTag:1;
UILabel *label = (UILabel *)[cell viewWithTag:2];
[imageView setImage:[UIImage imageNamed:#"XYZ"]];
[label setText:#"XYZ"];

If I interpret your question correctly, you want to have some sort of list of items, which can display in detail the information of each item.
I would take a look at UITableView, it is a class meant for displaying multiple rows of data, you can modify its behavior to display a view not unlike the one you describe here.
Xcode has several templates of iOS applications, of which one being an app with a tableview. I suggest opening up a sample project and reading a tutorial like this one: http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

Related

UIbutton with ControlEvents not working on uitableviewcell

This is my code
UIImageView *bgDetails = [[UIImageView alloc] initWithFrame: CGRectMake(0, 20, 320, 47)];
bgDetails.image = [UIImage imageNamed:#"bgDetail"];
UIButton *btnLike = [UIButton buttonWithType:UIButtonTypeCustom];
[btnLike setBackgroundImage:[UIImage imageNamed:#"38_38"] forState:UIControlStateNormal];
btnLike.frame = CGRectMake(278, 4.5f, 37, 37);
[btnLike addTarget:self action:#selector(doLike:) forControlEvents:UIControlEventTouchUpInside];
[bgDetails addSubview:btnLike];
[cell.contentView addSubview:bgDetails];
But btnLike does not call the doLike method. Please help me! Thanks!
Two possible issues here. The first one is this: I'm not sure whether it's just a typo, but you write
action:#selector(doLike:)
but then later you complain about the selector doLike which does not have the trailing colon (i. e. it takes no arguments). If this is the problem, you can fix it by changing either one, so that the two selector names match (they both need to be doLike: or doLike, consistently).
The other error I see is that you're adding the button as a subview of an image view. UIImageView has user interaction turned off by default, so in order it to react to user touches, you have to set its userInteractionEnabled property to enabled:
bgDetails.userInteractionEnabled = YES;

UIbutton on UItableView Cell not clickable

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
{
}

display label with UITableViewCellAccessoryDisclosureIndicator

I hope display label as below
So I used the codes below to display label with UITableViewCellAccessoryDisclosureIndicator
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero// cellframe //SimpleTableIdentifier1
reuseIdentifier:SimpleTableIdentifier1] autorelease];
cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;//
UILabel *labelCell =[ [[UILabel alloc]init] autorelease];
[labelCell setTag:11101];
labelCell.frame = CGRectMake(180.9f,15.0f,60.0f,23.0f) ;
[labelCell setBackgroundColor:[UIColor clearColor]];
labelCell.textAlignment=UITextAlignmentRight;
[labelCell setFont:[UIFont systemFontOfSize:13.0]];
[labelCell setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:labelCell];
UITableViewCellAccessoryDisclosureIndicator will display correctly
but the labelCell does not display, except I tap the row of the UITasbleViewCell
Welcome any comment
change the frame property of your UILabel,
try with changing value for x and y.
labelCell.frame = CGRectMake(180.9f,15.0f,60.0f,23.0f) ;
You could also consider to implement the accessory using accessoryView.
Implement a Custom Accessory View For UITableView in iPhone
Somehow I think doing this,
[cell.contentView bringSubviewToFront:labelCell];
will help but since there are other subviews in the table cell you will have to deal with their layout as well. Rather I suggest you create a new custom UITableViewCell subclass.

iphone - will it leak?

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?

How to create a button similar to "Remove Contact" button on iPhone?

Apple has many big colored buttons, that are not standard. One of them is a delete contact button in address book. Another is Start/Stop button in Timer (Clock application) or End Call button in Phone. They are different, but all have similar appearance.
Question is simple. Is there a way to create these kind of buttons without using background images/screenshots or recreating them from scratch?
Thanks.
You could try Opacity. The new release allows you to draw vector images and generate UIKit-friendly Objective-C code that recreates the drawing.
Well, I've tried many ways to do it, but the most simple was to make a UIButton with a custom style.
If you need a "delete" button in the footer of the table - like in contact or event, here is the code:
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UIButton *newDeleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
newDeleteButton.frame = CGRectMake(10, 10, 300, 43);
UIImage *buttonBackground = [UIImage imageNamed:#"ButtonDelete.png"];
[newDeleteButton setImage:buttonBackground forState:UIControlStateNormal];
[newDeleteButton addTarget:self action:#selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UILabel *deleteTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 39)];
deleteTitle.text = #"Delete";
deleteTitle.font = [UIFont boldSystemFontOfSize:20];
deleteTitle.textColor = [UIColor whiteColor];
deleteTitle.textAlignment = UITextAlignmentCenter;
deleteTitle.backgroundColor = [UIColor clearColor];
[footerView addSubview:newDeleteButton];
[footerView addSubview:deleteTitle];
formTableView.tableFooterView = footerView;
First, you create the view for footer.
Then you make a button with the correct background — just make a screenshot of any button you need and delete letters from it.
Than you create a caption for your button and place it over the button.
The last step is to put the button and caption in the footer view, and put the footer view into the table.