xcode view collection cell wrong size - cell

I have a custom collection cell nib that has the size w:160 h:146. When i populate my collection view, all the elements are extremely small, like 20px * 20px.
Why does it decrease the size of my cells ? This is my code
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)IndexPath
{
static NSString *CellIdentifier = #"Cell1";
[collectionView registerNib:[UINib nibWithNibName:#"TjansterCell" bundle:nil] forCellWithReuseIdentifier:#"Cell1"];
TjansterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:IndexPath];
return cell;
}

The issue is that the collection view flow layout is using the default size for its 'itemSize' property. Fortunately, this is easy to fix. Simply add the following code to your collection view's delegate:
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(<your width>, <your height>);
}

I had the same problem where my custom size remained the standard size when I ran the app. Therefore, to solve this problem I found by manually resizing the cell in storyboard first and then entering or re-entered the custom sizes in the size inspector solved this buggy problem.
Also, I found that when you select the collection view in storyboard and select the size inspector. Here you enter the correct dimensions under Collection View Size and enter the dimensions in the Cell size text fields. This is the important part.

Related

DART/FLUTTER: How to get access to member variable / values of Widgets

I have an list of widgets. The widgets are background images for a website. The last widget(image) in this list is displayed onscreen. As nav functions push/pop, the last element is removed/added.
One of the fields in the Widget(BackdropImage) is a bool called 'isForeground'. I need access to it.
Why? Some background images are foreground and are to be rendered above the site's semi-transparent background texture, some are to be rendered behind it.
If I have a List(BackDropImage) and the BackDropImage contains:
Path, BoxFit, Opacity, isForeground(bool) etc etc. How can I get access to the last BackDropImage in BackDropImage list and access its isForground field?
//PSUDO WIDGET TREE:
if(backdropImageList.last's 'isForground' field is FALSE) BackDropImage.last,//render here below the texture
BackgroundTextureWidget,
if(backdropImageList.last's isForground field is TRUE) BackDropImage.last //render here above the texture
HeadingWidget,
OtherWidgets
I'd appreciate help if possible (or alternative approaches). The alterative is to flip a bool programmatically every time a button is pressed/popped and keeping track of what images are displayed where. Knowing which images are fore/background in the first place and controlling from the object itself is much neater IMO.
Thanks folks.
list.firstWhere((x) => x['isForground'] == true);
Widget is an abstract class. When working with Widgets like Container etc that have member variables you need to access programmatically, it can be done like this:
Widget testContainer = Container (width:100)
print(testContainer.width) // WON'T WORK
print((testContainer as Container).width) // WILL WORK
To access the member variables of individual Widgets in a list of Widgets:
List<Widget> listOfWidgets = [Container(color:Colors.blue), Container(color:Colors.red), Container(color:Colors.green)]
void printColorsOfAllContainers () {
for (var element in listOfWidgets) {
if (element is Container) {
print(element.color);
}
}
}
Alternatively, you can also do things like this:
void printColorsOfAllContainers() {
final List<Color> listOfContainerColours = [];
for (var element in listOfWidgets) {
element as Container;
listOfContainerColours.add(element.color!);
}
}

In a typical list -> details SwiftUI app, what is the best way to create new elements?

In a typical list -> details SwiftUI view, I have basically an Array of objects, and for read/edit, I can easily bind these (using #Binding on the view) to view and edit the elements in the array.
What about adding new elements?
I would like to re-use my views for editing; but they expect an #Binding as I mentioned. How do I transition to this view if I want to allow the user to add a new element to the list?
One option is that I can pre-create an object before loading the view, and then binding the view to the new element. However, this makes "cancel" clunky (now I have to remove from the list). Also, it's not clear how to inject this logic (create a new element) in a NavigationLink.
Another option is that I can pass the list to the view and bind a constant empty object, and in the view I can manage adding the new element to the list upon successful creation.
What is the recommended approach to this? I see a lot of tutorials on how to edit and view lists, but not on how to add.
Sounds like you need a backing database. I used Apple's Core Data to add and retrieve stored objects to/from. Here's the guide I used: https://www.hackingwithswift.com/books/ios-swiftui/how-to-combine-core-data-and-swiftui
I figured out a way to do this that is quite nice.
What I've done is that in the list (say, LandmarkList), I added a default LandMark element (which represents the new element to add).
#Published var newLandMark : Landmark
I wrap my view with a new view, which binds against the list:
struct NewLandmarkView : LandmarkDetail {
#Binding var landmarkList : LandmarkList
}
This view adds buttons for save and cancel. Add takes the newLandmark and adds it to the list.
I then use the following to show modally (you can navigate to it if you want instead):
Button(action: {
// In the folder list view, we'll add a note to the "notes" folder
self.showModal = true
}) {
Image(systemName: "square.and.pencil")
}.sheet(isPresented: self.$showModal) {
CreateLandmarkView(landmarkList: self.$landmarkList)
}
This worked pretty well for me as a pattern.

Make famo.us / Angular scrollview with many elements per line

I have a beehive with many tiles and each tile is positioned by modifier as below :
My code looks like this :
<fa-scroll-view fa-pipe-from="scrollEventHandler" fa-options="scrollOptions">
<fa-view ng-repeat="tile in tiles">
<fa-modifier fa-transform="tile.transform">
<fa-surface fa-background-color="tile.color"/>
</fa-modifier>
</fa-view>
</fa-scroll-view>
and tile.transform return :
this.transform = function() {
var currentTilePosition = position.get();
return Transform.translate(currentTilePosition[0], currentTilePosition[1], currentTilePosition[2]);
}
Positions are correct when it's returned, but if I take a look in debug window, position are modified.
But if i set size in 'fa-modifier' (necessary to scroll), all my tiles are collapsed :
<fa-modifier fa-transform="tile.transform"
fa-size="[80, 75]">
My hexagons appear like this :
It looks like it is not possible to have multiple elements on the same line of scrollview and automatically pass the items to the next line.
Does someone already encountered this problem and solved it?
Famo.us scroller seems to be not optimized to manage multiple items in column and line.
Scroll-view is not able to calculate its size, so scrolling is not possible.
But it's possible to force scroll-view size to indicate how to scroll :
<fa-scroll-view fa-pipe-from="scrollEventHandler" fa-options="scrollOptions"
fa-size="[windowsWidth, windowsHeight]">
<fa-view id="globalView"
fa-size="[windowsWidth, globalScreenHeight]">
<fa-view ng-repeat="tile in tiles">
<fa-modifier fa-transform="tile.transform">
<fa-surface fa-background-color="tile.color"/>
</fa-modifier>
</fa-view>
</fa-view>
</fa-scroll-view>
I set Scroll view size which is screen size.
Then i created a global view which contains all object views. And i set scrollable size to this global view.
This works, but we do scroll-view job...

Dynamically set width and height of TextView in Android

I am trying to set TextView width dynamically, Using setWidth(width) method.
txtviewOne.setWidth(10);
txtviewTwo.setWidth(10);
But not success.
Please help me How can I set width of textview dynamically.
TextView tv;
----------------------
tv=new TextView(this); // or tv=new TextView(R.id.textview1);
LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(15,50);
tv.setLayoutParams(Params1);
I call new TableRow(), because textView's parent is TableRow. If your parent's view is LinearLayout, replace it by new LinearLayout.LayoutParams.
textView.setLayoutParams(
new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT));
I have experienced something similar in situations where there is a ScrollView in the parents hierarchy. No matter how I would modify layout inside it, my commands were ignored. If this is the case, try calling RequestLayout on the ScrollView or remove and re-add the child view of the ScrollView.

JList in JScrollPane messes up JPanel height

I have a weird problem that I can't seem to solve. I've got a JList which has to be able to scroll if there are more items in it then can be displayed. However if I put the JList in a JScrollPane, it doesn't utilise the full height of the EAST part of the BorderLayout it's in.
Example without JScrollPane:
public UsersPanel(){
String[] userList = new String[]{"Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar"};
JList users = new JList(userList);
add(users);
}
Example with JScrollPane:
public UsersPanel(){
String[] userList = new String[]{"Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar","Foo","Bar"};
JList users = new JList(userList);
JScrollPane sp = new JScrollPane(users);
add(sp);
}
I want a JList that utilises the full available height of the EAST part of the BorderLayout it's in. I've tried wrapping my JList inside another JPanel, but that doesn't solve my problem either.
use setVisibleRowCount() method to set number of visible rows in Jlist.
users.setVisibleRowCount(10);
One hack that I've used successfully before is to add a ComponentListener to parent component of the JList / JScrollPane, and as it changes reset the preferred size of the JList and/or Jscrollpane via (pseudo-code) setPreferredSize(getPreferredSize().width, parentHeight)