I checked many tutorials and I can't find what I want to do.
What I want to do, is storing GPS coordinates in my data base; so far no problem. In my tableViewControllerCell, I don't want use "subtitle" style, but use UILabel. My problem is I have this code which works well with "subtitle" style (see below). But I want to do with my own custom Label.
Can someone help me?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
let list = frc.objectAtIndexPath(indexPath) as! List
cell.textLabel?.text = list.item
var qty = list.qty
var note = list.note
cell.detailTextLabel?.text = "Qty:\(qty) -\(note)"
return cell
You will need to create a custom UITableViewCell for example myTableViewCell, assign it to the cell in the storyboard selecting the cell and selecting the class in the inspector (the same you did for you view controller)
After that when you retrieving the cell you must cast to you new class as below:
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! myTableViewCell
Now you can call your properties in the cell like
cell.myField = "value"
So basically you want to know how to use a custom view cell for your table view.
If you want to create a custom table view cell in storyboard:
Go to storyboard and select your table view (assuming you already have one).
In the field "Prototype Cells" change it to "1".
Add whatever you want to add in the prototype cell (displayed in the table view). In your case it would be extra labels.
Create a custom UITableViewCell class.
Go back to storyboard and assign the new custom table view cell class to your prototype. To do this, you'll have to select the cell (make sure it's the cell which is selected by checking in the left pane of the storyboard. Click the "Identity inspector" and assign the class to the cell.
While the cell is still selected, go back to "Attributes inspector" and give it an identifier in the "Identifier" field. Something like "cell" should be enough.
Click the Assistent editor and ALT click on your custom table view cell file.
Create outlets for your labels in this custom table view cell class. It should be looking like this:
class CustomTableViewCell: UITableViewCell {
#IBOutlet weak var labelName: UILabel!
#IBOutlet weak var labelCustom: UILabel!
}
In your view controller you can now add things to your cell as shown below:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell
let data = something[indexPath.row]
cell.labelName.text = data.name
cell.labelCustom.text = String(data.number)
return cell
}
Related
Hi guys i'm new in this group, i want to create an app that have a tableview cotroller as main controlle and when touch the cell open a tableview in a uiview controller that have a button to add item. How can i do to make a correct flow of data?
In order to do that, several things need to be done.
1.a) In the storyboard, create your tableView Controller and your viewController. Create also a cocoa touch file for both of your controllers.
1.b) Inside the ViewController, add a object 'tableView' and add an outlet for this object.
Then in the 'ViewDidLoad' method of your ViewController, set tableView.delegate = self and tableView.dataSource = self.
Your ViewController has to inherit from 2 protocols (class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource) This will add two functions, to handle the datas displayed in the tableView of your second ViewController.
1.c) Create a generic segue (by control dragging from the tableView Controller to the ViewController) and name it 'YourSegue'
2) In the tableview controller, override the function: override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) and add the following line: performSegue(withIdentifier: "YourSegue", sender: nil) which will open the second ViewController.
3) If, additionally, you wanna send datas from the first TableView Controller to your second ViewController, you should override the method 'prepare(for segue:)' which allows you to instantiate variables of your secondviewController.
Hope this helps.
I have a bound WPF ListBox with SelectionMode="Extended". When I select more than one item, using the Ctrl key, listBox.SelectedItems returns the items in the order in which they were selected. I need them in the original order. The items don't contain a way to determine that.
Do I need to wrap the items in another class which has an IsSelected property and somehow set that from the ListBox, and then iterate through the whole ItemsSource collection?
Or is there a simpler way?
You can use LINQ to re-apply the original order. It can be done in two steps
1. project your ListBox's items to Dictionary using Select (there's an overload which'll give you the index)
2. Match your selected items against indexed collection and then sort them by the index.
here's some backing code:
var items = new[] { "A", "B", "C", "D" }; // your original items source
var selectedItems = new[] { "D", "C" }; // selection in any order
var indexedItems = items.Select((item, index) => new KeyValuePair<int, string>(index, item)); // indexed items
selectedItems = selectedItems.OrderBy(t => indexedItems.Single(t2 => t2.Value == t).Key).ToArray(); // selected items in the right order
MessageBox.Show(selectedItems[0]);
With XCode 4.6 building for iOS 6.1, I'm using a UICollectionView from a storyboard, and I thought that the class implemented scrolling by default. I have made a simple test app with a storyboard that has just a Collection View and one Collection View Cell, and code that implements just collectionView:cellForItemAtIndexPath: and collectionView:numberOfItemsInSection:, calling for enough cells so that scrolling is required to see all the cells.
I thought that with nothing else, I would be able to scroll vertically within the UICollectionView. It shows the visible cells but doesn't scroll. What am I missing here?
Why wouldn't this component allow me to scroll vertically, what am I unable to see?
The implementation code is about as simple as possible:
//
// TestpViewController.h
//
#import <UIKit/UIKit.h>
#interface TestpViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>
#end
//
// TestpViewController.m
//
#import "TestpViewController.h"
#interface TestpViewController ()
#property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
#end
#implementation TestpViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return 30;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"testCell" forIndexPath:indexPath];
return cell;
}
#end
I am loading a UITableView cell from a nib file in a UITableViewController and the background color that I set in Interface Builder is not being displayed. Why not?
You should use tableView:willDisplayCell:forRowAtIndexPath: to set a background color, as Apple recommends in the Table View Programming Guide for iOS:
A table view sends a tableView:willDisplayCell:forRowAtIndexPath: message to its delegate just before it draws a row. If the delegate chooses to implement this method, it can make last-minute changes to the cell object before it is displayed. With this method, the delegate should change only state-based properties that were set earlier by the table view, such as selection and background color, and not content.
This code does an alternating "zebra stripe" effect:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2 == 0) {
UIColor *altCellColor = [UIColor colorWithWhite:0.7 alpha:0.1];
cell.backgroundColor = altCellColor;
}
}
In my app there are some UICollectionViewCells displaying some info.
When the user taps a button on one of them, I flip the tapped cells with this piece of code:
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
[UIView animateWithDuration:1.0
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^
{
NSLog(#"starting animation");
[UIView transitionFromView:cell.contentView
toView:cell.contentView
duration:.5
options:UIViewAnimationOptionTransitionFlipFromRight
completion:nil];
}
completion:^(BOOL finished)
{
NSLog(#"animation end");
}
];
After the cell flips (which is does correctly) the cell is completly white.
Two questions about is:
- why is the cell white after the flip. Shouldn't it display the original info since the fromView is equal to the toView?
- what is the best way to display different content on the back of the cell. I suppose UICollectionViewCell doesn't have something link cell.contentViewBack...
you may have this by now but,
Not sure if this is the 'Best' way to do it, I got this to work by creating a custom UICollectionViewCell, having 2 UIImageViews in the custom cell, and targeting those views in the animation (this only works for you if thats all you want to have in your cells - may help, may not, anywho)
Create new UICollectionViewCell class (mine is called CardCVCell)
In the CardCVCell.h put in you UIImageView outlets
#property (strong, nonatomic) IBOutlet UIImageView *cardImage;
#property (strong, nonatomic) IBOutlet UIImageView *cardBackImage;
I used storyboard - in there I typed in 'CardCVCell' as my custom class on the cell in the Collection View in my scene.
In my View Controller for that scene I have the code you have above, but I use the UIImageViews in the custom cell for the views in the transition (note you have to cast the UICollectionViewCell to your custom class
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// animate the cell user tapped on
CardCVCell *cell = (CardCVCell *)[collectionView cellForItemAtIndexPath:indexPath];
[UIView transitionFromView:cell.cardBackImage
toView:cell.cardImage
duration:.5
options:UIViewAnimationOptionTransitionFlipFromRight
completion:^(BOOL finished)
{
if (finished) {
NSLog(#"animation end");
}
}
];
}
I hope this helps someone if not you, if I can help let me know.
cheers