iOS: How to check if a label exists in a certain area? - ios

In my application I include a lot of different labels (they represent street names). Therefore I do some calculation to show them in the right angle and on the right spot. Now I want to check wether there is an existing label with the same text in the area where I want to include the new one, but I'm not sure how to manage this. Can I check for an intersection maybe?
I'm looking forward for help

You can fetch UILabel from UIView in following manner -
for (id view in self.view.subviews) {
if ([view isKindOfClass:[UILabel class]]) {
UILabel *label = (UILabel *) view;
}
}

Related

iOS in Tableview textfield get value in for loop is not working in ios 7 but its working in iOS 8

I have get tableview textfield value on click on button , I lots of try but i can't able to get value Please help me here is my code its always print null value, this for loop run on
Click On button but this code is not working in ios 7 simulater but its working with ios 8 Please help me
for (int k=0; k< aryModifier.count; k++) {
// NSLog(#"--%d",i);
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:k inSection:0];
UITableViewCell *cell = [[self tbl_itemModifier] cellForRowAtIndexPath:indexPath];
UITextField *getTextView = (UITextField*)[cell.contentView viewWithTag:200];
//for (UIView *subView in cell.subviews)
for (UIView *subView in cell.contentView.subviews)
{
if ([subView isKindOfClass:[UITextField class]])
{
UITextField *txtField = (UITextField *)subView;
NSLog(#"%#",txtField.text);
}
}
//UITextField *getTextView = (UITextField*)[cell.contentView viewWithTag:i];
NSLog(#"%#",getTextView.text);
}
and Table Cellforrow at index path
UITextField *txt=[[UITextField alloc]initWithFrame:CGRectMake(180, 40, 50, 15)];
txt.text = #"1";
txt.textAlignment = NSTextAlignmentCenter;
txt.font=[UIFont systemFontOfSize:12];
txt.textColor = [UIColor blackColor];
txt.tag = 200;//indexPath.row;
txt.delegate = self;
NSString *str = txt.text;
[cell addSubview:txt];
I try both way but i cant get success . Please help me .........
//for (UIView *subView in cell.subviews)
for (UIView *subView in cell.contentView.subviews)
Please give me solution ...
if you have any demo link please share with me
There are several issues with the approach you're using:
Creating a new control in tableView:cellForRowAtIndexPath: is bad practice. Proper MVC would dictate moving that creation to your UITableViewCell's init or initWithCoder: method. Doing it in the tableView:cellForRowAtIndexPath: method is extremely likely to result in multiple controls being added to a cell as that cell is recycled. That method is really intended only for cell configuration, not view hierarchy creation/management.
Iterating UITableViewCell objects is a bad idea. The reason is that iOS aggressively manages memory consumption when creating UITableViewCell instances for display. It recycles cells as often as it can. Therefore, if you have a table view with many cells, only a small portion of them will be available at any given time. You should be using an external data source to provide the data that the table view uses to populate its cells. You should also be using this to grab the data you need, not relying on controls in a UITableViewCell for that data.
A few questions for you:
- How are you currently populating your cells?
- Is there other data that is displayed in the cells, or are they just a scrolling set of UITextField instances?
- Is this basically just a scrollable form?
Assuming (based on the limited evidence provided) that you're trying to implement a form, here are my suggestions:
Don't use a UITableView. I know it's tempting because it handles the keyboard show/hide well and will (sometimes) provide auto-scrolling to the selected control, but it's really trying to fit a square peg in a round hole. It's not what a UITableView is made to do. Instead, learn how to correctly implement forms in a UIScrollView. In particular, this become much easier for me when I embraced a "full auto layout implementation" methodology. It's a little bit more complicated to set up initially, but it works much better and makes tasks like yours a lot more logical.
If you must use a UITableView, figure out a way to persist data on-the-fly to a separate data source that is used to populate your UITableView. That way, as cells are recycled, you can refresh them with correct data, and when you need to access the entered data, you have no need to iterate UITableViewCell instances (which is fairly "expensive" to do).
Change below line of code:
UITextField *getTextView = (UITextField*)[cell.contentView viewWithTag:200];
to
UITextField *getTextView = (UITextField*)[cell viewWithTag:200];
Hope this helps..

How to remove Views from UITableView custom row on PrepareForReuse?

I have a UITable view with a random number of UIImageView in every Single row. In the costruction of the single row, I've used [self addSubView: xxx] and a for cycle to add every UIImageView i need to add (my model has an array of URL). But now I've noticed that when the UITableView reuse the rows and it doesn't clean the UIImageViews added.
I've tied to manually remove them in method onPrepareForReuse as in the code:
if(_messageContentsFrames != nil){
for(NVChatMessageContent *singleContent in _messageContentsFrames){
[singleContent removeFromSuperview];
}
}
But it gives me error. How can i completely reset the view when it is going to be reused?
You can remove these imageViews in -tableView:cellForRowAtIndexPath: method. That always worked for me.
By the way, do you really need to reuse cells if you make such work with it. Wouldn't it be easier to create new cell?
Or it would be better not to remove imageViews, but reconfigure them on reusing and remove those views, that cell doesn't need.
Option 1. Set tag to your UIImageView (use: imageView.tag = 123456) and then in tableView:cellForRowAtIndexPath: you should get the specific view using:
id imageView = [self.view viewWithTag:yourInteger];
[imageView removeFromSuperview];
Option 2. Use:
for( UIView *view in cell.subviews) {
if ([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperView];
}
}

Get UILabel from UIView

I have added a number of labels to a view giving them tags.
What is the correct way to retrieve a label from the view. I am wanting to re-postion the label. Here is what I am using to retrieve the label and re-position it:
UILabel *theLabel = (UILabel *)[self.view viewWithTag:5];
[theLabel.layer setPosition:CGPointMake(100, 200)];
Is this the correct way of doing it?
There's no need to go to the layer level, although it might work fine.
theLabel.center = CGPointMake(100,200);
I guess that does the same thing, without looking at the documentation to verify that a layer's default anchor point is its center.
You can use fast enumeration with a for-loop:
for (UIView *view in [self.view subViews]) {
if([view isKindOfClass:[UILabel class]]) {
// do your stuff here
}
}
Try this, it will surely work.
To retrieve the label, use an IBOutlet if you've created it in Interface Builder, or a raw property or ivar if created in code. There's no need to loop the subviews or use viewWithTag if you've got a direct reference (outlet, property, ivar) to it.
To move the label, directly set its frame or center property without needing to access its layer.

How to remove content from UITableViewCell but still have separator left?

In my app, i have table view in which i am reloading data based on Business logic so it was overlapping earlier then i write this piece of code to remove content before drawing:
for(UIView *view in cell.subviews){
if ([view isKindOfClass:[UIView class]]) {
[view removeFromSuperview];
}
}
And now its removing everything including cell separators. Any idea how to prevent it??
Thanks,
Remove subviews from [cell contentView]
If you don't know what are you doing, you should never add views or remove views from the cell directly. Use only contentView, backgroundView and selectedBackgroundView.
However, there is rarely a use case when you should remove all the views from a cell. Why don't you just create a new cell (or reuse one)? Or just set the hidden property to YES for the subviews?

Labels are lingering in my reusable table cells

I have a table whose cells contain labels. Whenever I dequeue a reusable cell, the old labels are still lingering on it. I was able to remove them with this:
for(int a=[[newcell subviews]count]-1; a>=0;a--)
{
if([[[[newcell subviews]objectAtIndex:a]class] isSubclassOfClass:[UILabel class]])
{
[[[newcell subviews] objectAtIndex:a] removeFromSuperview];
}
}
But when I select the cell, I can see the old text on top of the new. I tried this:
[[newcell.selectedBackgroundView subviews] makeObjectsPerformSelector: #selector(removeFromSuperview)];
[[newcell.backgroundView subviews] makeObjectsPerformSelector: #selector(removeFromSuperview)];
But it didn't work. How can I make the old labels disappear from the selected cell as well as the regular view of the cell?
Subclass UITableViewCell (if you aren't already). Override prepareForReuse and remove the labels there. Might work
This kind of problem tends to happen when you add subviews to your cells in cellForRowAtIndexPath: regardless of whether it's being dequeued or newly created. As a result, you end up creating a new subview each time the row is reused, and the old subviews accumulate.
What you instead want to do is to use the same subview each time, but just set the relevant attributes (e.g., labels or color) each time. Check out the answers to How do I clear a cell completely when I reuse it? to see some possible approaches.
I kinda did what Yuji suggested. Instead of putting in new labels on each iteration, I checked whether the cell contained labels and then either edited the labels if they were there or put them in if they weren't. Code goes like this:
if([[newcell.contentView subviews] count]>=2 && [[[[newcell.contentView subviews] objectAtIndex:0]class] isSubclassOfClass:[UILabel class]] &&
[[[[newcell.contentView subviews] objectAtIndex:1]class] isSubclassOfClass:[UILabel class]])
{
//change the text of the labels
}
else
{
//add the labels to the cell
}

Resources