Awesome menu in a table cell detect click - ios

Hi im coding an awesome menu button on each cell, the problem is that I need to know which Awesome Menu view I´m clicking
This is my function to add the Awesome Menu view to each cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ActivasTableViewCell";
ActivasTableViewCell *cell = (ActivasTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ActivasTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
UIImage *storyMenuItemImage = [UIImage imageNamed:#"bg-menuitem.png"];
UIImage *storyMenuItemImagePressed = [UIImage imageNamed:#"bg-menuitem-highlighted.png"];
UIImage *starImage = [UIImage imageNamed:#"icon-star.png"];
AwesomeMenuItem *starMenuItem1 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
highlightedImage:storyMenuItemImagePressed
ContentImage:starImage
highlightedContentImage:nil];
AwesomeMenuItem *starMenuItem2 = [[AwesomeMenuItem alloc] initWithImage:storyMenuItemImage
highlightedImage:storyMenuItemImagePressed
ContentImage:starImage
highlightedContentImage:nil];
NSArray *menus = [NSArray arrayWithObjects:starMenuItem1, starMenuItem2,nil];
AwesomeMenuItem *startItem = [[AwesomeMenuItem alloc] initWithImage:[UIImage imageNamed:#"bg-addbutton.png"]
highlightedImage:[UIImage imageNamed:#"bg-addbutton-highlighted.png"]
ContentImage:[UIImage imageNamed:#"icon-plus.png"]
highlightedContentImage:[UIImage imageNamed:#"icon-plus-highlighted.png"]];
AwesomeMenu *menu = [[AwesomeMenu alloc] initWithFrame:self.view.bounds startItem:startItem optionMenus:menus];
menu.delegate = self;
menu.menuWholeAngle = M_PI_2*3;
menu.farRadius = 50.0f;
menu.endRadius = 50.0f;
menu.nearRadius = 30.0f;
menu.animationDuration = 0.3f;
menu.startPoint = CGPointMake(290.0,166.0);
[cell addSubview:menu];
return cell;
}
This are my functions for detecting a click on the view
- (void)awesomeMenu:(AwesomeMenu *)menu didSelectIndex:(NSInteger)idx
{
NSLog(#"Select the index : %d",idx);
}
- (void)awesomeMenuDidFinishAnimationClose:(AwesomeMenu *)menu {
NSLog(#"Menu was closed!");
}
- (void)awesomeMenuDidFinishAnimationOpen:(AwesomeMenu *)menu {
NSLog(#"Menu is open!");
}

There are a couple of approaches you could take.
If you only have one section in your table, you could assign the tag property of each AwesomeMenu object to the value of whatever indexPath.row is in -tableView:cellForRowAtIndexPath:.
You could add a property to AwesomeMenu for the purpose of identifying the row or indexPath it is located at:
#interface AwesomeMenu : NSObject
...
#property (nonatomic) int rowIndex; // If you only need the row index
#property (nonatomic, strong) NSIndexPath *indexPath; // If you need row and section
...
#end
Or, you could get the cell for the AwesomeMenu and then get its indexPath from the table:
- (void)awesomeMenuDidFinishAnimationClose:(AwesomeMenu *)menu {
UITableViewCell *cell = [self getTableViewCellForView: menu];
NSIndexPath *indexPath = [self.myMenuTable indexPathForCell: cell];
}
- (UITableViewCell *) getTableViewCellForView: (UIView *) view {
UIView *parentView = view;
while (parentView) {
if ([parentView isKindOfClass: [UITableViewCell class]]) {
return (UITableViewCell *) parentView;
}
parentView = parentView.superview;
}
return nil;
}

Related

Collapsable Cell

I have a UITableView with two different custom table cells. The first cell appears normal after I start the app. The second cell will appear when you click on them.
Can anybody help me or has an idea?
Thanks a lot.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = #"customCell2";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor grayColor];
cell.textLabel.font = [UIFont fontWithName:#"STHeitiSC-Light" size:9.0];
}
return cell;
}
Having done custom UITableViewCell in the past I usually handle the nib loading in the custom class itself.
The basic header for the custom cell.
#interface RequestsTableViewCell : UITableViewCell {
// Ivars.
}
// Properties.
- (id) initWithRequestModel: (RequestModel *) model style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier forQueryType:(int) requestType;
// Other methods, etc.
#end
The custom cell with a designated initializer.
#implementation RequestsTableViewCell
- (id) initWithRequestModel: (RequestModel *) model style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier forQueryType:(int) requestType {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:#"RequestsTableViewCell" owner:self options:nil];
self = [nibArray objectAtIndex:0];
requestModel = model;
queryType = requestType;
[self setRequestThumbnail];
[self setRequestCategory];
[self setRequestAddress];
[self setRequestStatusDate];
[self setRequestStatus];
[self setRequestFollowed];
[self setRequestComment];
[self setAppearance];
}
return self;
}
There would also be a custom xib for the custom UITableViewCell that corresponds and has the custom class set in the identity inspector.
In the UITableViewController.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"Cell Id";
RequestModel *request = nil;
// Other code for search, etc
request = [self.serviceRequests objectAtIndex:indexPath.row];
RequestsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if(!cell) {
cell = [[RequestsTableViewCell alloc] initWithRequestModel:request style:UITableViewCellStyleDefault reuseIdentifier:cellId forQueryType:queryTypeIndicator];
}
return cell;
}
It also sounds like you have more than one custom cell type in your question? Can you elaborate on how it is all supposed to function? You say that you have to click one cell to make another appear, can you explain that interaction?
I did something similar, but made the cell 'expand', instead of adding a new cell. Of course then you don't have two cells, but you can resize your one cell, add subframes,...
You can keep a boolean in your UITableViewCell object (BOOL cellIsExpanded), and set that on tap gesture. Then in drawRect of the TableViewCell, layout your cell accordingly.
Example code, on expand, make cell height 20-->80 and add a UIButton:
In the TableViewController, overload heightForRowAtIndexPath (this will resize your cell if 'expanded'):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
YourEntity *record = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (!record.cellIsExpanded)
return 20.; // cell is smaller if collapsed
else
return 80.; // bigger cell
}
In the TableViewCell, add or remove subframes:
#interface MyTableViewCell ()
#property(nonatomic) BOOL cellIsExpanded
#property(strong, nonatomic) UITextField *myTextField;
#property(strong, nonatomic) UIButton *clickMeButton;
#end
#implementation MyTableViewCell
- (void)drawRect:(CGRect)rect {
if(!self.cellIsExpanded){
// layout your collapsed cell, for example:
self.myTextField = [[UITextField alloc] initWithFrame:self.frame];
self.myTextField.text = #"Collapsed cell";
// remove button, only present in expanded view :
self.clickMeButton=nil;
}
else{
self.myTextField.text = #"Expanded cell";
// add button below textfield
self.clickMeButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 10, 10)];
}
}
#end

How come my UITableView is repeating rows after 5 rows?

I have 7 rows in my database. I confirmed that all of the data is successfully coming over to the iOS side by NSLog the postArray which gives 7. However when I run my app, it will only display the first 5 rows, and then the first 2 rows instead of the 6th and 7th row from my database. Also when I NSLog the actual text from my 6th and 7th text view the correct text is in there... Why is it repeating after 5 rows? Thank you. Here is my code:
#import "DailyViewController.h"
#import "Post.h"
#interface DailyViewController ()
#end
#implementation DailyViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// View background
[self.view setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(221/255.0) blue:(85/255.0) alpha:1.0f]];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://example.org/getPosts.php"]];
NSData *data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
postArray = [[NSMutableArray alloc] init];
for(int i = 0; i < jsonArray.count; i++)
{
NSString *nickname = [[jsonArray objectAtIndex:i] objectForKey:#"nickname"];
NSString *squeal = [[jsonArray objectAtIndex:i] objectForKey:#"squeal"];
[postArray addObject:[[Post alloc] initWithNickname:nickname andSqueal:squeal]];
}
viewArray = [[NSMutableArray alloc] init];
for(int i = 0; i < postArray.count; i++)
{
Post *postObject;
postObject = [postArray objectAtIndex:i];
UILabel *nicknameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 15, 320, 30)];
nicknameLabel.text = postObject.nickname;
nicknameLabel.font = [UIFont boldSystemFontOfSize:20];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(25, 42, 320, 0)];
textView.font = [UIFont systemFontOfSize:15];
textView.text = postObject.squeal;
textView.editable = false;
[textView setScrollEnabled:false];
[textView sizeToFit];
[textView layoutIfNeeded];
UIView *view = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 320, 30+textView.frame.size.height)];
[view addSubview:nicknameLabel];
[view addSubview:textView];
[viewArray addObject:view];
}
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return postArray.count;
}
- (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:CellIdentifier];
[cell.contentView addSubview:[viewArray objectAtIndex:indexPath.row]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *view = [viewArray objectAtIndex:indexPath.row];
return view.frame.size.height+30;
}
#end
UITableView reuses the cells with the same reuse identifier, which means that when you scroll so that a row is scrolled out of the visible area, it will be used to show new rows. That's why when your 1st and 2nd rows were scrolled out of the view, they were used to show the 5th and 6th row.
You need to modify how you load cells.
- (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:CellIdentifier];
// Add and setup views here.
}
//Add content to view here
// e.g. cell.textLabel.text = #"Some text";
return cell;
}
BTW You can use the property textLabelof UITableViewCellinstead of creating a new label and adding it as as subview. If you need to have more control over your custom cell, then you should create a custom class and use a Storyboard file or a Xib file.
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else
{
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
}
[cell.contentView addSubview:[viewArray objectAtIndex:indexPath.row]];
In your cellForRowAtIndexPath method, you are adding a subview to the cell's content view only if the cell is being initialized for the first time.
Rather, you should add/replace the subview on each call to cellForRowAtIndexPath.

Get segmentedcontrol value from tableviewcells

How do i get the value (the selected state) from my segmentedcontrols in the tableviewcells?
When i press the button "Get states" it should return the value for each of the segmented controls. I have tried different methods, but I can't find one that works :(
My code so far:
- (void)viewDidLoad
{
[super viewDidLoad];
tableData = [[NSMutableArray alloc] initWithCapacity:0];
tableData = [NSArray arrayWithObjects:#"First", #"Second", #"Third", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"StateCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"StateCell"];
}
//Config cell..
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
NSArray *itemArray = [NSArray arrayWithObjects: #"1", #"2", #"3", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(110, 7, 100, 28);
[cell.contentView addSubview:segmentedControl];
return cell;
[[self tableView] reloadData];
}
- (IBAction)getStates:(id)sender {
// Ruturn the current selected statement, for the individual cell's segmentedcontrol..
// Ex. First = SelectedState 1, Second = SelectedState 0 & Third = SelectedState 2..
}
So what I'm really is asking for; is what the "Get states" button action has to do..
Thanks for your time!
Your code has a couple of problems. Most of them happen because a UITableView reuses its cells.
You are creating a new UISegmentedControl each time a cell is displayed, which you should not. You should create the UISegmentedControl only if you create the cell, move that code into the cell == nil).
You don't have a dataSource that saves the state of the segments. You should not save states in views, especially not if you are dealing with a tableView, because cells are reused.
Here's an example that will get the functionality you need.
// this is an object of your model, it has a title and saves the selected index
#interface MBFancyObject : NSObject
#property (strong, nonatomic) NSString *title;
#property (assign, nonatomic) NSInteger selectedIndex;
#end
#implementation MBFancyObject
#end
#interface MasterViewController () {
NSMutableArray *_objects; // stores instances of MBFancyObject
}
#end
#implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
// set up the model
_objects = [NSMutableArray array];
for (NSInteger i = 0; i < 6; i++) {
MBFancyObject *object = [[MBFancyObject alloc] init];
object.title = [NSString stringWithFormat:#"Object #%ld", (long)i];
object.selectedIndex = i % 3;
[_objects addObject:object];
}
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:#"Get States" style:UIBarButtonItemStyleBordered target:self action:#selector(logStates:)];
self.navigationItem.rightBarButtonItem = button;
}
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"FancyCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"FancyCell"];
// add the segmentedControl when you create a new cell
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:#[#"1", #"2", #"3"]];
segmentedControl.frame = CGRectMake(110, 7, 100, 28);
[cell.contentView addSubview:segmentedControl];
// add an action so we can change our model if the view changes
[segmentedControl addTarget:self action:#selector(didChangeSegmentedControl:) forControlEvents:UIControlEventValueChanged];
// use a tag so we can retrieve the segmentedControl later
segmentedControl.tag = 42;
}
// either if the cell could be dequeued or you created a new cell,
// segmentedControl will contain a valid instance
UISegmentedControl *segmentedControl = (UISegmentedControl *)[cell.contentView viewWithTag:42];
MBFancyObject *object = _objects[indexPath.row];
cell.textLabel.text = object.title;
segmentedControl.selectedSegmentIndex = object.selectedIndex;
return cell;
}
- (IBAction)didChangeSegmentedControl:(UISegmentedControl *)sender {
// transform the origin of the cell to the frame of the tableView
CGPoint senderOriginInTableView = [self.tableView convertPoint:CGPointZero fromView:sender];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:senderOriginInTableView];
NSAssert(indexPath, #"must have a valid indexPath");
MBFancyObject *object = _objects[indexPath.row];
object.selectedIndex = sender.selectedSegmentIndex;
}
- (IBAction)logStates:(id)sender {
// query the model, not the view
for (NSInteger i = 0; i < [_objects count]; i++) {
MBFancyObject *object = _objects[i];
NSLog(#"Object \"%#\" - %ld", object.title, (long)object.selectedIndex);
// since you have only one section, each indexPath is 0,i
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
}
}
#end
Use an array to store all the segment control values, and when click one segment control then just change the value correspondingly.
You have serious problem with reusing here
Do not ever allocate a new UI element in tableView:cellForRowAtIndexPath: method unless it is in the if condition if (cell == nil)
Change What in Your tableView:cellForRowAtIndexPath: to be
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"StateCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"StateCell"];
// Add the Segmented Control
NSArray *itemArray = [NSArray arrayWithObjects: #"1", #"2", #"3", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(110, 7, 100, 28);
segmentedControl.tag = 1;
[cell addSubview:segmentedControl];
}
//Config cell..
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
// Get that Segmented Control
UISegmentedControl *segmentedControl = (UISegmentedControl *)[cell viewWithTag:1];
segmentedControl.selectedSegmentIndex = 0; // Set your default value here or add your data in an array and read from that array
return cell;
Then in the action of the button do that
for (UITableViewCell *cell in [tableView visibleCells]) {
UISegmentedControl *segmentedControl = (UISegmentedControl *)[cell viewWithTag:1];
NSLog(#"%d",segmentedControl.selectedSegmentIndex);
}
However this code is not perfect unless you have only 3 cells in your table to avoid reuse or visibility problem

I can't make corners of images and buttons rounded

I'm creating the cells of a UITableViewController.
In one of them, a small image is contained. I tried the following to make its corners rounded:
profileImage.layer.cornerRadius = 8;
profileImage.clipsToBounds = YES;
In another cell prototype, I tried to make the corners of a button rounded:
chooseImageFromRoll.clipsToBounds = YES;
chooseImageFromRoll.layer.cornerRadius = 8;
In both cases I included
#import <QuartzCore/QuartzCore.h>
The button and the image whose corners must be rounded are property of the UITableViewController owning them:
#import <UIKit/UIKit.h>
#interface profileRegistrationCellVC : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *profileImage;
#property (weak, nonatomic) IBOutlet UIButton *chooseImageFromRoll;
#property (weak, nonatomic) IBOutlet UIButton *shootImage;
#end
In relative .m class:
#import "profileRegistrationCellVC.h"
#import <QuartzCore/QuartzCore.h>
#implementation profileRegistrationCellVC
#synthesize profileImage;
#synthesize chooseImageFromRoll;
#synthesize shootImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
chooseImageFromRoll.clipsToBounds = YES;
chooseImageFromRoll.layer.cornerRadius = 8;
shootImage.layer.cornerRadius = 8;
profileImage.layer.cornerRadius = 8;
profileImage.clipsToBounds = YES;
profileImage.layer.borderColor = [UIColor whiteColor].CGColor;
profileImage.layer.borderWidth = 20.0;
[self addSubview:profileImage];
[self addSubview:chooseImageFromRoll];
[self addSubview:shootImage];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Here's the code of my function cellForRowAtIndexPath, in my uitableviewcontroller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
static NSString *CellIdentifier = #"profileRegistrationCell";
profileRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[profileRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//cell.profileImage.image.layer.masksToBounds = YES;
//cell.profileImage.layer.cornerRadius = 5.0;
return cell;
}
else if (indexPath.section == 1) {
static NSString *CellIdentifier = #"regularRegistrationCell";
regularRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[regularRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.regularFieldName.text = [_registrationItems objectAtIndex:indexPath.row];
if ([[_registrationItems objectAtIndex:indexPath.row] isEqualToString:#"Email*"])
cell.regularTextField.keyboardType = UIKeyboardTypeEmailAddress;
if ([[_registrationItems objectAtIndex:indexPath.row] isEqualToString:#"Età"]) {
cell.regularTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
}
return cell;
}
else{
static NSString *CellIdentifier = #"orientationRegistrationCell";
orientationRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[orientationRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.fieldLabel.text = [_registrationItems objectAtIndex:[_registrationItems count]-1];
cell.orientationLabel.text = #"Non specificato";
return cell;
}
}
But in no case I managed to make corners rounded. Can you tell where I'm mistaking?
Thanks
The code snippets above are both correct and have no issues. My assumption is that your issue lies elsewhere in creating the button that contains the image. The following code will create a button and will round it's corners, also I added the border to it in case you want to add that too. You can plug in the image you have in there as well. I have added a image of what this code will create for your reffrence also. Hope it will help you out.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100,50);
[btn setTitle:#"Hello" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor colorWithRed:128.0/255.0f green:0.0/255.0f blue:0.0/255.0f alpha:0.7]];
btn.frame = CGRectMake(100.0, 100.0, 120.0, 50.0);//width and height should be same value
btn.clipsToBounds = YES;
btn.layer.cornerRadius = 20;//half of the width
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=2.0f;
[self.view addSubview:btn];
Below is the image of the button that is related with the above code
Edit:
another way of doing the round corners is to use the method masksToBounds here is an example of it within the generic cell right out of the box from the template.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
cell.imageView.image = [UIImage imageNamed:#"acolon.png"];
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
return cell;
}
here is the screen shot of the result:
i know you using a custom cell, so implement the maskToBount in the cellForRowAtImdexPath or wherever you are populating the tableview with its custom cells.

How to add images to UITableView cell?

I temporarly added some values to the UITableViewCell programmatically. But I need to add images to each cell. How can I do that?
Here is my code. .h file
#interface BidalertsViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {
NSArray *listData;
}
#property(nonatomic, retain) NSArray *listData;
#end
.m file
#synthesize listData;
- (void)viewDidLoad {
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(5,0,310,28)];
newView.backgroundColor=[UIColor whiteColor];
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 25.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.font = [UIFont systemFontOfSize:15];
mytext.text = #"Asset Name";
[mytext release];
[newView addSubview:mytext];
[self.view addSubview:newView];
[newView release];
NSArray *array = [[NSArray alloc] initWithObjects:#"iPhone", #"iPod", #"iPad",nil];
self.listData = array;
[array release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[listData dealloc];
[super dealloc];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:#"user.jpg"];
[cell.contentView addSubview:imv];
[imv release];
}
#end
The code didn't add images to the cell. What's the problem? How can I do to add custom image to cell?
Move return cell; at the end of the method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:#"user.jpg"];
[cell.contentView addSubview:imv];
[imv release];
return cell;
}
You can add images using the imageView property of a cell:
cell.imageView.image = [UIImage imageNamed:#"user.jpg"];
in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.
Here's an example cell loading a facebook image in a table cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Leave cells empty if there's no data yet
if (nodeCount > 0)
{
// Set up the cell...
AppRecord *appRecord = [self.friendsList objectAtIndex:indexPath.row];
cell.textLabel.text = friend.name;
// Only load cached images; defer new downloads until scrolling ends
if (!appRecord.icon)
{
// set the url
appRecord.imageURLString = [NSString stringWithFormat:#"http://graph.facebook.com/%#/picture", friend.recordID];
// request the download
if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
// if a download is deferred or in progress, return a placeholder image
cell.imageView.image = [UIImage imageNamed:#"Placeholder.png"];
}
else
{
cell.imageView.image = appRecord.icon;
}
}
return cell;
you adding it right, but you fogot one thing:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row]; cell.textLabel.text = [listData objectAtIndex:row];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:#"user.jpg"];
[cell.contentView addSubview:imv];
[imv release];
//you fogot this:
return cell;
}
// create a rectangle box for image view
UIImageView *iconImage = [[UIImageView alloc] init];
iconImage.frame = CGRectMake(12,4,53.5,53.5);
// Create a label for cells
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)];
cellName.font = [self fontForBodyTextStyle];
NSString *cellNameStr = [NSString stringWithFormat:#"%#",self.tableCellNames[indexPath.row]];
// Select path row tab actions
switch (indexPath.row) {
case 0:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"condition.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 1:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"videos.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 2:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"provider.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 3:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"info.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
default:
NSAssert(NO, #"Unhandled value in cellForRowAtIndexPath");
break;
}
Thank VKJ,

Resources