Hi I am New in iOS Development I added UITableViewCell With Custom Cell Design in which I
added two labels in one Cell for which I added
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(/* where you want it*/)];
label.text = #"text"; //etc...
label.autoresizingMask=UIViewAutoresizingFlexibleWidth;
[self.contentView addSubView:label];
[label release];
where my First Label is masking but second Label is Sticking while repositioning table
I am Using iOS6
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
I want to change UILabel text color in initWithStyle method of UITableViewCell. But color is not getting changed.
But when color changing is done in cellForRowAtIndexPath method,color is getting changed. Why?
Depending how you are using your label you just need to set textColor property:
For creating tableViewCell in - (UITableViewCell*)tableView:(UITableView *)tableView method I think you have used like following:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
there may be possibility that you have your style is either UITableViewCellStyleDefault or UITableViewCellStyleValue1 orUITableViewCellStyleValue2orUITableViewCellStyleSubtitle`.
Now whatever style you are using if you are adding your own label than use:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,25)];
lbl.text = #"your Text";
[lbl setTextColor:[UIColor greenColor]];
and if you are not adding your own labels and using default label which is textLabel or detailTextLabel of UITableViewCell object:
[cell.textLabel setTextColor:[UIColor greenColor]];
[cell.detailTextLabel setTextColor:[UIColor greenColor]];
If you are adding any label in subview like:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,25)];
lbl.text = #"your Text";
[lbl setTextColor:[UIColor greenColor]];
you can replace greenColor with your desire color.
Hope this helps :)
//selected rows is mutablearray
if (aCell.accessoryType == UITableViewCellAccessoryCheckmark) {
[selectedRows addObject:indexPath];
}
Problem here is when I scroll uitableview, all selection is messed up. What is the best way keep track of selected cells? I also do not want to use checkmark I just want to change background color of selected rows.
If you want to change the selected color of the selected cell.cell.selectedcolor = [uicolor blackcolcor];
or you can use this method for change background color of the custom tableeview cell
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
I have a UITableViewController initialized with the grouped style and having multiple sections. For one of these sections, I'd like its constituent cells to be completely transparent and have no border. I plan to assign a custom view for every row in this section, but having that custom view surrounded by the grouped table cell looks bad :(
The following makes the background color of a cell black instead of transparent... And I still don't know how to get rid of the border.
cell.backgroundColor = [UIColor clearColor];
Any pointers? Thanks!
NOTE: This doesn't appear to be working in iOS7 and above. For iOS7 try this answer.
For iOS6 and below, to remove the grouped background from a cell in a grouped table view cell:
This didn't work
cell.backgroundView = nil; // Did Not Work
This did
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
If you have moved to ARC (I've heard this works, but haven't tested it)
cell.backgroundView = [UIView new];
You have to actually set
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
to remove the border of cells.
The following hack works in iOS 7 – for now. :)
Subclass UITableViewCell, and use this cell for the section that shouldn't have separators.
Override the addSubview method in your cell subclass:
-(void)addSubview:(UIView *)view
{
// The separator has a height of 0.5pt on a retina display and 1pt on non-retina.
// Prevent subviews with this height from being added.
if (CGRectGetHeight(view.frame)*[UIScreen mainScreen].scale == 1)
{
return;
}
[super addSubview:view];
}
This is what worked for with having a Grouped style table
[tableView setSeparatorColor:[UIColor clearColor]];
This code worked for me :)
[self.tableView setSeparatorColor:[UIColor clearColor]];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Set the backgroundView of the cell to nil. For a grouped table, the cell image is part of that view.
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
Try using tableView.separatorColor = [UIColor clearColor];
And, don't use tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
I tested with both, if style is none, making the section borders invisible is not working, but instead just change its color, and section border will appear to be none.
iOS seems to be differentiating making an object none and making an object transparent
cell.backgroundView = [UIView new];
Works like a charm! Tested! iOS6
As of iOS 8, setting the separator attribute to none works as well.
Setting a content view also gets rid of the border. Set your custom view to cell.contentView.
The easiest way to remove cell borders from a section of grouped-style UITableView:
[tableViewOutlet setBackgroundView:nil];
in the viewDidLoad method.
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
cell.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:imageView];
If you have a custom UITableCellView then you can add the following method to your view to remove the background view.
- (void)setBackgroundView:(UIView *)backgroundView
{
// We don't want background views for this cell.
[super setBackgroundView:nil];
}
I just thought I would convert my comment to #Intentss into an answer, because it maybe useful for those, using his solution.
Using iOS6.1 with a grouped UITabelView, using ARC:
[tableView setSeparatorColor:[UIColor clearColor]];
Does not work
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
Does work