outlet collection removefromsuperview - uiview

I have an outlet collection, and tried to remove one of the views by accessing it from the collection array and calling removefromsuperview. This ended up removing all the views that were attached to the collection from the interface builder. Any help would be appreciated!
assignments.removeAtIndex(i).removeFromSuperview()
https://www.dropbox.com/s/pglhob9uyflv8kn/ass.tiff?dl=0

assignments.removeAtIndex(i).removeFromSuperview() you're first removing an object from the assignments array, and then you're calling removeFromSuperview on the whole array.
All it looks like you should have to do is assignments[i].removeFromSuperview()

Related

Problem of adding to an outlet collection

I am developing an iOS application and I have, in one of my controllers, an outlet collection of UIImageView.
Each imageView inside has a different tag. Only when I loop over the collection to get their tag, only the tag of the first element I added inside appears..
Here is the relevant part of the code :
#IBOutlet var imageLine1: [UIImageView]!
for image in tabImage {
print(image.tab)
switch image.tab {
// Here I do some stuff
}
}
And now the display in the Xcode terminal :
6
And the first image I added to this table has a tag of 6. So I conclude that I didn't manage to add the other images (which I do with a control drag on the outlet collection).
Does anyone have any idea what the problem is/ how to fix it?
Namely that I already had a similar collection, which contained all the images, which I deleted to replace it with the one above. They had the same name and the same elements inside, maybe that interfered with the correct functioning.
Thanks for your help!
When you delete the old outlet collection you have to reconnect them to the new outlet collection within Interface-Builder.
Also make sure to delete the references to the old outlet connection within IB.

iOS: UICollectionView content reset and dequeueReusableCell

Okay the situation goes as follows:
I have a collection view where in cellForRow I am using dequeueReusableCell to reuse the cells. On each cell I have a custom UIView object that is added as a subview.
Now, under a certain circumstance I must re-layout the collection view entirely. When this happens
Clear all item from data model
Call deleteItems for all visible cells' index paths
Call reloadData
At this point the collection view is empty and there are no cells displayed.
Now if I update my model again with data and reload the collection view - In cellForRow dequeueReusableCell returns reused cells/the added UIView as explained above is there!/- it does not initialize new cell objects even though the collection view was empty before the current update. I am not sure if this is the expected behaviour or I have some other problem in my code, however my question is - how can get to a point where I reset all the content on the collection view and dequeueReusableCell returns a newly initialized cell object.
I have learnt this the hard way! Save yourself some time Petar by knowing this that In any collection view/tableview, anytime there is any change of constraint or any UI element in its layout structure, you should ALWAYS, and ALWAYS make another prototype cell. It is the ONLY correct way, so don't think you ll be putting some more time in making another cell.
As I read your comment where you said you ll initiliaze another view based on that one condition where you want to reset everything. Just have another cell prototype with that another view and dequeue that now.
Hope it solves your problem

How can I reuse a View object?

I have that view object with some elements. I want to call that view , and can use it in another view, having a list of objects view one below the other. The problem, I do not know how I can create like a component.
Regards
Create a subclass of UIView, and do what you need to in the view, and every time you want to use this kind view, just use an instance of this class.
I think you want to use a reusable UIView, so in your case I think you will create a xib in Xcode.
Regards, Jorge.
Have a look to Container view in interface builder

I was confused by immutable copy and mutable copy in Objective-C

Oddly, there is no command for removing all of a view’s subviews at once. However, a
view’s subviews array is an immutable copy of the internal list of subviews, so it is legal
to cycle through it and remove each subview one at a time:
for v in myView.subviews as [UIView] {
v.removeFromSuperview()
}
This content is in the Programming IOS 8,if the copy is immutable copy ,why it can change?
I don't think you are a really asking about immutable arrays here as you aren't invoking any methods on the array itself, so it's mutability cannot be an issue. The immutable attribute of the subviews array is how the view has decided to present the list to you. It's got nothing to do with how sub-views interact with parent views.
You appear to be confused about why a subview can remove itself from the parent view and you cannot; this is simply because the subview is a UIView-subclass and the parent view is a UIView-subclass and therefore the subview has access to all of the internal variables of the parent and can do whatever it likes to the parent. You cannot. This is deliberate as you don't know the intricacies of the view hierarchy (and don't want to), where as the UIView obviously does.
Another interesting aspect of the code you posted is that often getting an element in an array to remove itself from the array while you are enumerating it, will cause an exception. In this particular case, however the subviews array you receive from the view is a copy of the original (an immutable copy) and therefore getting the subview to remove itself from the parent view will not affect this array and the enumeration will not falter. Thanks to Christopher Kevin Howell for pointing this out, as I missed it completely, first time round.
The subviews array is immutable, so you can't change it. For example, you can't remove an element of the array yourself, or overwrite it with a completely new array.
However, there's nothing stopping the internal implementation of the class from changing it by overwriting it internally.
In this case though, the subviews array that is returned is a copy of the actual subviews array.

MonoTouch: UITableView outlet variable is null

I am running through the "Navigating with tables" section of Wrox' Professional iPhone Programming with MonoTouch by McClure et al, picking up the basics of putting together a hierarchical UI for iOS, and running into the following problem.
I have created a new "iPhone View with Controller" file (called ParametersViewController), deleted the UIView from it, added a UITableView, created an outlet for it (tableView) and connected the "File's Owner" view outlet to the UITableView, per the tutorial.
In the RowSelected method of this view's parent view, I instantiate my ParametersViewController, calling the default constructor, in which I want to set up the table view's data source:
this.tableView.Source = new DataSource(this, new [] {"one", "two", "three"});
(DataSource is a nested class which inherits from UITableViewSource)
All compiles and runs fine, up until the point where that line is executed. Turns out that this.tableView is null, so I get a NullReferenceException.
tableView is the outlet for the table. How can it be null? Can't I set up the table source here in the constructor? If not, where do I do it?
Solution to this was
Don't create an outlet called tableView
Make the view a subclass of UITableViewController, instead of UIViewController
Refer to the base.TableView property of my UITableViewController, rather than the nonsense outlet I created
I'm not 100% clear on what you're doing where, but it might be that you need to access your UITableView in the UIViewController's ViewDidLoad method. At that point your NIB view(s) should be instantiated.
If that's not the problem, it may help if you could provide some more code so that we can see exactly what's going on. I think the lack of responses might due to people not being sure that they understand the nature of your problem exactly.

Resources