when I add label in UItablevewcell if it is as nil in contentview of cell.If it is not nil,
I am taking that label through tag without allocating.it is the right procedure to reuse the cell.
but when I dont want that label in second row , i have to hide it.How can I remove the label in second row only
without hiding.I need it in first row.
For example you can use different cell identifiers when dequeue and create them. #"Cell With Label" and #"Cell Without Label" for instance.
Or you can tag this label by label.tag = MY_INT_TAG and search it by UILabel *label = [cell viewWithTag:MY_INT_TAG] to remove it from super view in the second row. It works when you don't want to subclass UITableViewCell.
if (indexPath.row == 0) {
UILabel *label = [[UILabel alloc] init];
label.tag = TAG;
[cell.contentView addSubview:label];
} else if (indexPath.row == 1) {
UILabel *label = [cell.contentView viewWithTag:TAG];
[label removeFromSuperView];
}
When you are reusing the cells which have no common elements, the best practice is to clear the cell subviews (all added elements) before reusing it.
This way you can add the elements each time depending on your needs...
You can do this:
for(UIView *view in cell.contentView.subviews){
[view removeFromSuperview];
}
or if you want to be more fancy:
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
Of course if you want to clear only 1 particular element in a particular row, then you must assign a unique tag to the element when you add it to the cell's contentview, then remove it by accessing it through it's tag value:
Add it to the cell:
UIImageView *rightArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"]];
rightArrow.tag = 111;
rightArrow.frame = CGRectMake(290, 16, 4, 8);
[cell.contentView addSubview:rightArrow];
Remove it from the view for row 2:
if (indexpath.row == 2) {
UIImageView *rightArrow = (UIImageView *)[cell.contentView viewWithTag:111];
if (rightArrow)
[rightArrow removeFromSuperView];
}
Related
Here is my code,
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
if ([cell.textLabel.text isEqualToString:#"Small" ]) {
cell.imageView.image=nil;
cell.accessoryView = nil;
cell.accessoryType=UITableViewCellAccessoryCheckmark;
return cell;
}
But checkmark is adding to the right corner of the cell
I want to add it to another position inside the cell, place like below,
[[chkmrk alloc]initWithFrame:CGRectMake(200, -4, 100, 50)];
try the following code. Actually the accessory View is button in the cell. Make a custom cell and inside that cell position the AccessoryView layout in the CustomCell.m file in -layoutSubviews Method. The code is as following:
- (void) layoutSubviews
{
[super layoutSubviews];
UIView * arrowView = nil;
for (UIView* subview in self.subviews)
{
if ([subview isKindOfClass: [UIButton class]])
{
arrowView = subview;
break;
}
}
CGRect arrowViewFrame = arrowView.frame;
arrowViewFrame = CGRectMake(200, -4, 100, 50);
arrowView.frame = arrowViewFrame;
}
This will help you sure.
You could hack it - but safer is to just add new subview with the desired image.
[cell.contentView addSubview:yourAccesorView];
You can control position of that by setting frame.origin.x, y
No it is not possible to change position of accessoryView of UITableViewCell. You need to customize your cell.
Such like add button in cell.contentView and put image (check/uncheck image) on Button and manage (check or uncheck) it on its click event by button tag.
Add a custom buttom to your cell where you want, and add an action too. If you do this you can add the button everywhere you want to be placed in the cell.
UIButton* yourCustomAccessoryButton = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
[cell addSubview:yourCustomAccessoryButton];
I have a problem, how i can show the line between check mark and move at the time of tableview edit. like same as in below image.
explain the process or else if possible please send me to example code.
Thanks in advance.
Look at this :
Declare BOOL checkMark; in .h
check its value.
// This all code in Cell For Row At IndexPath :
for (UIView *ObView in [cell.contentView subviews])
{
[ObView removeFromSuperview];
}
if(checkMark == YES) // you can check here by imdexPath.row.
{
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10,5,150,30)];
lbl.text = #"";
[cell.contentView addSubview:lbl];
UIImageView *CheckImage = [[UIImageView alloc]initWithFrame:CGRectMake(160,5,20,20)];
CheckImage.image = [UIImage imageNamed:#""];
[cell.contentView addSubview:CheckImage];
UIImageView *BarImage = [[UIImageView alloc]initWithFrame:CGRectMake(185,5,0.5,30)];
BarImage.image = [UIImage imageNamed:#""];
[cell.contentView addSubview:BarImage];
UIImageView *tblImage = [[UIImageView alloc]initWithFrame:CGRectMake(160,5,20,20)];
tblImage.image = [UIImage imageNamed:#""];
[cell.contentView addSubview:tblImage];
}
else
{
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10,5,150,30)];
lbl.text = #"";
[cell.contentView addSubview:lbl];
UIImageView *tblImage = [[UIImageView alloc]initWithFrame:CGRectMake(160,5,20,20)];
tblImage.image = [UIImage imageNamed:#""];
[cell.contentView addSubview:tblImage];
}
Note: frames are temporary.
SO you basically have a tableView and you want to add that image "|" in your tableViewCell when you are editing.
I would assume you already have editingEnabled for your tableView, and you will need to implement
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
if (editing) {
// Configure self.tableView and individual cells and add the "|" as a subView.
// It will all be tricky, and you should be able to logically place the vertical bar
// But whatever you want to be in your EDIT mode, you will have to configure it here.
} else {
// Re-configure and implement the 'correct' cells back to normal tableView
}
}
Create a UIView with 1px as width and grey as backgroundColor, and add this to the right of check mark should be fine.
If you Edit option is an image then you can do the code like below to show a line.
Steps
1] First import QuartzCore/QuartzCore.h
2] set border width and border color property of your Edit image.
[editImageView.layer setBorderWidth:2.0];
[editImageView.layer setBorderColor:[[UIColor grayColor] CGColor]];
//If you putting a button for edit then also you can set the same. Just replace the editImageView with your editButton
Hope you get exact you want!:)
Take uiimageview in table cell and set its width to 1. Set height according to your tablename.cell.height.
It will definitely solve your problem
You can customize your tableview cell.Just add the subviews at the time of cell creation and put it hidden untill click on edit button.
I have the following code which draws a separator line and text for a UITableViewCell. It looks fine but when I scroll off screen then back, the separator line is gone but the text is still fine. Any ideas?
static NSString *aProgressIdentifier = #"CustomerCell";
UITableViewCell *aCustomerCell = [iTableView dequeueReusableCellWithIdentifier:aProgressIdentifier];
if (!aCustomerCell) {
aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
aCustomerCell.contentView.backgroundColor = [UIColor whiteColor];
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[aCustomerCell addSubview:aLine];
[aLine release];
}
CMACustomer *aCustomerObject = aCellObject;
aCustomerCell.textLabel.text = aCustomerObject.customerFullName;
aCustomerCell.detailTextLabel.text = nil;
aCell = aCustomerCell;
Try to add the "aLine" image view as subview of the contentView and not the whole table itself. Probably when the cell is reused and then layoutSubviews is called again, the contentView overlaps (white background) your aLine. Infact consider that iOS default cells have their subviews dynamically redrawn and resized each time they are displayed on screen.
So I would try this:
[aCustomerCell.contentView addSubview:aLine];
If this doesn't work, what can you do is to remove the contentView completely and add your own custom subviews (do this inside the if(!aCustomerCell) and not outside unless you will not get the benefits of the cell re-use):
if (!aCustomerCell) {
aCustomerCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
[cell.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[aCustomerCell.contentView addSubview:aLine];
[aLine release];
}
Finally another check is verify that the cell height is > 72 (it seems a trivial check but its often source of headaches!).
The table view is using a pool of cells, so you can't be sure which one you're getting for any given index path. You can use the cell or the content view, but be sure to add only one of your custom lines per cell.
UIImageView *aLine = (UIImageView *)[cell viewWithTag:64];
if (!aLine) {
// etc.
UIImageView *aLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 72, 800, 1)];
aLine.tag = 64;
[cell addSubview:aLine];
//
}
// other formatting logic here, you can also hide/show aLine based on biz logic
try adding it to the contentView
[aCustomerCell.contentView addSubview:aLine]
example if there is a method addLabel:
- (void)addLabel {
for (NSInteger i = 0; i < 5; i ++) {
UILabel *label = [[UILabel alloc] init];
[label setText:#"label"];
[[self view] addSubView:label];
[label release];
}
}
and the method is called whenever a button is fired.
Does it need to remove all the label from the subviews first (removeFromSuperView:) before addSubview again?
First, you have to give some coordinate to UILabel. So that, it can display at proper place.
You can use following line for that:
UILabel *lblTaskTitle = [[UILabel alloc] initWithFrame:CGRectMake(45.0, 5, 200.0, 35.0)];
Another thing is that, it will be better if you remove other label. (It's not necessary, but it's good practice).
You can do it in following way:
UILabel *lbl = nil;
NSArray *Arraylbl = [self.view subviews];
for (lbl in Arraylbl){
if ([lbl isKindOfClass:[UILabel class]]){
[lbl removeFromSuperview];
}
}
Hope it will be fine for you.
Let me know in case of any difficulty.
Yes you have to remove all the previous labels from super view otherwise they all will be added above the previous existing labels, so the new labels would not be understandable.
I want to add a table header (not section headers) like in the contacts app for example:
exactly like that - a label beside an image above of the table.
I want the all view be scrollable so I can't place those outside of the table.
How can I do that?
UITableView has a tableHeaderView property. Set that to whatever view you want up there.
Use a new UIView as a container, add a text label and an image view to that new UIView, then set tableHeaderView to the new view.
For example, in a UITableViewController:
-(void)viewDidLoad
{
// ...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:imageView];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
[headerView addSubview:labelView];
self.tableView.tableHeaderView = headerView;
[imageView release];
[labelView release];
[headerView release];
// ...
}
You can do it pretty easy in Interface Builder. Just create a view with a table and drop another view onto the table. This will become the table header view. Add your labels and image to that view. See the pic below for the view hierarchy.
In Swift:
override func viewDidLoad() {
super.viewDidLoad()
// We set the table view header.
let cellTableViewHeader = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewHeaderCustomCellIdentifier) as! UITableViewCell
cellTableViewHeader.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewHeaderCustomCellIdentifier]!)
self.tableView.tableHeaderView = cellTableViewHeader
// We set the table view footer, just know that it will also remove extra cells from tableview.
let cellTableViewFooter = tableView.dequeueReusableCellWithIdentifier(TableViewController.tableViewFooterCustomCellIdentifier) as! UITableViewCell
cellTableViewFooter.frame = CGRectMake(0, 0, self.tableView.bounds.width, self.heightCache[TableViewController.tableViewFooterCustomCellIdentifier]!)
self.tableView.tableFooterView = cellTableViewFooter
}
You can also simply create ONLY a UIView in Interface builder and drag & drop the ImageView and UILabel (to make it look like your desired header) and then use that.
Once your UIView looks like the way you want it too, you can programmatically initialize it from the XIB and add to your UITableView. In other words, you dont have to design the ENTIRE table in IB. Just the headerView (this way the header view can be reused in other tables as well)
For example I have a custom UIView for one of my table headers. The view is managed by a xib file called "CustomHeaderView" and it is loaded into the table header using the following code in my UITableViewController subclass:
-(UIView *) customHeaderView {
if (!customHeaderView) {
[[NSBundle mainBundle] loadNibNamed:#"CustomHeaderView" owner:self options:nil];
}
return customHeaderView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the CustomerHeaderView as the tables header view
self.tableView.tableHeaderView = self.customHeaderView;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
headerView.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
headerView.layer.borderColor=[UIColor blackColor].CGColor;
headerView.layer.borderWidth=1.0f;
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5,100,20)];
headerLabel.textAlignment = NSTextAlignmentRight;
headerLabel.text = #"LeadCode ";
//headerLabel.textColor=[UIColor whiteColor];
headerLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel];
UILabel *headerLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, headerView.frame.size.width-120.0, headerView.frame.size.height)];
headerLabel1.textAlignment = NSTextAlignmentRight;
headerLabel1.text = #"LeadName";
headerLabel.textColor=[UIColor whiteColor];
headerLabel1.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel1];
return headerView;
}