Can't set the custom static cell - ios

I would like to set a custom cell so I created the CustomDetailCell class and a cell on the storyboard where I've created 2 UILabel. For the labels I change the font and the size and connect the custom class with the cell on the storyboard. I try to use it like this:
case DetailControllerAddressSection:
{
CustomDetailCell *customCell = (CustomDetailCell *)[tableView dequeueReusableCellWithIdentifier: #"AddressCell" forIndexPath: indexPath];
customCell.addresLabel.text = [self.objectData pointSubtitle];
customCell.distanceLabel.text = [self distanceMessageForObjectData: self.objectData];
return customCell;
break;
}
But on my cell there is only one label with the other font (not like in storyboard cell)
Can't understand this.. Any help?
PS The identifier for the cell is used in storyboard.
EDIT: Custom controller
#import <UIKit/UIKit.h>
#interface CustomDetailCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *addressLabel;
#property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
#end
#import "CustomDetailCell.h"
#implementation CustomDetailCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
#end

Related

How to pass indexPath from button inside footer to segue?

I'm working on a custom layout collectionView which has quilt look and each section has a footer. Two buttons are inside footer and each button has a segue to another view(I set segues using IB).
Footer has its own indexPath which was assigned in a UICollectionViewFlowLayout class(Where I made a custom layout). The indexPath of footer is like this - {section - row} {0-0} {1-0} {2-0}....
I want to keep this indexPath information to use at the another view which is linked to two buttons.
I tried "convertPoint: toView: ... indexPathForRowAtPoint" way but didn't work. I think it's because the buttons are not belong to collectionViewItem but belong to Footer.
In this situation, what is the best way of passing each footerView's indexPath to segue?
Any solution with code example will be greatly appreciated.
// CollectionView DataSource
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath {
Footer *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:#"Footer"
forIndexPath:indexPath];
// set button images and labels
[footerview setButton];
// debug code : show current section number
footerview.footerLabel.text = [NSString stringWithFormat:#"Section %li", (long)indexPath.section];
return footerview;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:#"SeeAllPictures"]) {
// how to pass indextPath?
}
else if([[segue identifier] isEqualToString:#"GoDiary"]) {
// how to pass indextPath?
}
}
Below is header of Footer class
#import <UIKit/UIKit.h>
#interface Footer : UICollectionReusableView
#property (strong, nonatomic) IBOutlet UILabel *footerLabel;
#property (strong, nonatomic) IBOutlet UIButton *seeAllPictures;
#property (strong, nonatomic) IBOutlet UIButton *goDiary;
#property (strong, nonatomic) IBOutlet UIImageView *seeAllPicturesImage;
#property (strong, nonatomic) IBOutlet UILabel *seeAllPicturesLabel;
#property (strong, nonatomic) IBOutlet UIImageView *goDiaryImage;
#property (strong, nonatomic) IBOutlet UILabel *goDiaryLabel;
- (void)setButton;
#end
try the following:
imagine a reusable view with two buttons (leftButton and rightButton). in your reusable view classes .h file add a property for the indexpath:
#property (strong, nonatomic) NSIndexPath *indexPath;
then in your viewcontrollers viewForSupplementaryElementOfKind method:
CustomFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:#"FooterView" forIndexPath:indexPath];
footerView.indexPath = indexPath;
[footerView.leftButton addTarget:self action:#selector(leftButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[footerView.rightButton addTarget:self action:#selector(rightButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
then implement the two methods:
- (void)leftButtonTapped:(UIButton *)sender {
[self performSegueWithIdentifier:#"LeftButtonSegue" sender:sender];
}
- (void)rightButtonTapped:(UIButton *)sender {
[self performSegueWithIdentifier:#"RightButtonSegue" sender:sender];
}
and finally in your viewcontrollers prepareForSegue method:
CustomFooterView *footerView = (CustomFooterView *)((UIButton *)sender).superview;
NSLog(#"perform segue from indexpath %ld, %ld", (long) footerView.indexPath.section, (long) footerView.indexPath.row);
IMPORTANT
the buttons have to be direct childs of the reusable view to make
(CustomFooterView *)((UIButton *)sender).superview
work. and of course you have to connect the segues LeftButtonSegue and RightButtonSegue in storyboard.

Prototype cell won't show content

I have designed a cell in storyboard, then created a custom UITableViewCell subclass (BasicCell) and assigned it to it. Also set the proper identifiers. I created outlets of the custom cell in that class. Then in my UITableViewController subclass (where I have two types of prototype cells), the cells content won't show up. What am I doing wrong?
BasicCell.h
#interface BasicCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *scorevalue;
#property (strong, nonatomic) IBOutlet UILabel *avgvalue;
#property (strong, nonatomic) IBOutlet UILabel *rankvalue;
#property (strong, nonatomic) IBOutlet UILabel *scorelabel;
#property (strong, nonatomic) IBOutlet UILabel *categorylabel;
#property (strong, nonatomic) IBOutlet UILabel *ranklabel;
#end
TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifierBasic = #"basic";
static NSString *CellIdentifierDetailed = #"detailed";
UITableViewCell *cell;
if(indexPath.row==0){
// Create first cell
cell = [[BasicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierBasic];
}
else{
// Create all others
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierDetailed forIndexPath:indexPath];
}
// Configure the cell...
switch ([indexPath row])
{
case 0:
{
BasicCell *basicCell = (BasicCell *)cell;
basicCell.scorelabel.text = #"test";
basicCell.categorylabel.text = #"test";
basicCell.ranklabel.text = #"test";
basicCell.scorevalue.text = #"test";
basicCell.avgvalue.text = #"test";
basicCell.rankvalue.text = #"test";
}
}
In your storyboard,
Create two prototype cell.
Change one of your prototype cell's custom class. Set it BasicCell.
Make sure that you set appropriate cell identifier for each type of cell.
Don't alloc init if it's indexPath.row == 0 but dequeue with appropriate identifier.
It should work.
Note: You should declare your IBOutlets as weak not as strong. Cell's view has already strong pointer to them. You don't have to own them.

How to get the multiple selected CollectionViewCell from the UICollectionView?

I create a xib file and add a Collection View Cell. I add the progress bar in header file of cell like the following code.
#import <UIKit/UIKit.h>
#import "MobileVLCKit/VLCMediaThumbnailer.h"
#interface AITFileCell_grid : UICollectionViewCell <VLCMediaThumbnailerDelegate>
#property (strong, nonatomic) IBOutlet UIImageView *fileIcon;
#property (strong, nonatomic) IBOutlet UILabel *fileName;
#property (strong, nonatomic) IBOutlet UILabel *fileSize;
#property (strong, nonatomic) NSURL *filePath ;
+ (NSString *)reuseIdentifier ;
#end
#interface AITFileCell_grid()
{
BOOL fetching ;
#public
UIProgressView *dlProgress;
UILabel *dlProgressLabel;
}
#end
I also create another xib file and add the Collection View for loading Collection View Cell.
I want to selected multiple file and hide the progress bar on each cell by button.
But I can not get the cell when I click the delete button by following code.
- (IBAction) buttonCancel:(id)sender {
if(self.collectionView.indexPathsForSelectedItems > 0){
for(NSIndexPath *indexPath in self.collectionView.indexPathsForSelectedItems){
AITFileCell_grid *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:#"AITFileCell_grid" forIndexPath:indexPath];
//Hide the progress bar
cell->dlProgressLabel.text = #"0%";
cell->dlProgress.hidden = YES;
cell->dlProgressLabel.hidden = YES;
}
}
[self setEditing:NO animated:YES];
}
I set the breakpoint to the cell->dlProgressLabel.text = #"0%"; , it show the nil in the log...and the progress bar didn't change anything.
But I can set the value to cell->dlProgressLabel.text and set value to cell->dlProgress in NSTimer.
Why the cell define in the UIButton seems not be efficient ?...
Can someone help and teach me how to solve ?
You are dequeuing Cell by doing
AITFileCell_grid *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:#"AITFileCell_grid" forIndexPath:indexPath];
which could be different from what you have been selected, instead get the selected cell from the collectionView
AITFileCell_grid *cell = [self.collectionView cellForItemAtIndexPath: indexPath]

Custom UITableViewCell Class Issue

I've read quite a few tutorials online about how to create a custom cell subclass, but I'm still a little confused. When I try to follow the instructions found in this question, I end up with errors that the tableView is not a valid property for the object of ViewController.
I've created a new subclass of UITableViewCell, called CustomBookClass. I've hooked up the properties with my CustomBookClass.h file.
#import <UIKit/UIKit.h>
#interface CustomBookCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UIImageView *bookImage;
#property (weak, nonatomic) IBOutlet UILabel *bookTitle;
#property (weak, nonatomic) IBOutlet UILabel *dateAdded;
#end
I then go into my ViewController.m file to edit the viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView.delegate = self];
[self.tableView.dataSource=self];
[self.tableView registerClass:[CustomBookCell class]forCellReuseIdentifier:#"Custom
Cell"];
}
I get an error on the tableView, saying the property doesn't exist, even though in the ViewController.h file, I'm including the table view.
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource>
#end
I'm sure I'm missing something really obvious here, as this is my first time trying this. Can anyone help me out? Thanks!
UIViewController does not have that property. Even if you assign the Delegate and DataSource protocols to it. There are a couple of things you can do however.
Link the table view to the ViewController yourself. That means, create a property/outlet called tableView.
Inherit from UITableViewController instead of UIViewController.
Both should work.
Edit: Oh and the lines:
[self.tableView.delegate = self];
[self.tableView.dataSource=self];
don't make sense. They should be either:
self.tableView.delegate = self; and self.tableView.dataSource = self;
Or [self.tableView setDelegate:self]; and [self.tableView setDataSource:self]
with 1 being preferred. (Edit: option #2 was actually incorrect code, my bad.)
No need to set delegate and datasource in TableViewCell class
simple make a property and synthesize your table view item
#property (weak, nonatomic) IBOutlet UIImageView *bookImage;
#property (weak, nonatomic) IBOutlet UILabel *bookTitle;
#property (weak, nonatomic) IBOutlet UILabel *dateAdded;
now import your cell class in your tableView controller class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
//If you are using xib for representing your UI then add a nib file.
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.bookTitle.text = [tableData objectAtIndex:indexPath.row];
cell.bookImage.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.dateAdded.text = [prepTime objectAtIndex:indexPath.row];
return cell;
}
for more detail check this link

UITableViewCell custom IBOutlet connection crashes

I made a custom view for a tablecell, i have some elements in the cell, i have coded the correct IBOutlet conenctions etc etc.
When I do not make any IBOutlet connections, I get to see the custom cell i made, so the class and reference are all good.
The moment I make a connection to an element, and compile i get:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<SearchViewController 0x1d05f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key image.'
the .h file...
again, without connections it works (but again I cannot modify the elements which is the whole idea of a custom cell) I get this error after connecting to IBOutlet...
I have also tried to connect 1 by 1, but they all give an error in the same range
CustomTableCell.h
#import <UIKit/UIKit.h>
#interface CustomTableCell : UITableViewCell
{
IBOutlet UIImageView *image;
IBOutlet UILabel *labelHeader;
IBOutlet UILabel *labelDescription;
IBOutlet UIButton *labelButtonPrice;
IBOutlet UIButton *labelButtonSize;
IBOutlet UIButton *labelButtonFloor;
IBOutlet UIButton *labelButtonBeds;
IBOutlet UIButton *labelButtonBaths;
}
#property (nonatomic) IBOutlet UIImageView *image;
#property (nonatomic) IBOutlet UILabel *labelHeader;
#property (nonatomic) IBOutlet UILabel *labelDescription;
#property (nonatomic) IBOutlet UIButton *labelButtonPrice;
#property (nonatomic) IBOutlet UIButton *labelButtonSize;
#property (nonatomic) IBOutlet UIButton *labelButtonFloor;
#property (nonatomic) IBOutlet UIButton *labelButtonBeds;
#property (nonatomic) IBOutlet UIButton *labelButtonBaths;
#end
CustomTableCell.m
#import "CustomTableCell.h"
#implementation CustomTableCell
#synthesize image;
#synthesize labelHeader;
#synthesize labelDescription;
#synthesize labelButtonPrice;
#synthesize labelButtonSize;
#synthesize labelButtonFloor;
#synthesize labelButtonBeds;
#synthesize labelButtonBaths;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
#end
tableview from search table
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomTableCellIdentifier = #"CustomTableCell";
CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CustomTableCellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell_iPhone" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
//cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
//cell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row];
return cell;
}
Now, when i make no connecitons at all, i get to see the custom cell,
when i make a connection it stops.
This problem can happen when you have your custom cell in a xib file and connect your IBOutlet to "File owner" object, not to cell itself.
Then when you try to load your cell from xib with your view controller as file owner for a xib it tries to connect image to non-existing property of controller.
Try to check your outlet connections and make sure they are connected only to your cell, not to any other objects

Resources