UILabel displaying (null) in custom UItableviewcell - ios

I can't figure out why my UILabels in my custom TableViewCell are nil in CellForRowAtIndexPath. I used the same method as I did for a previous TableView in another view Controller. Here's what I already checked:
Cell Style is Custom
Cell has a unique identifier "profilecell"
Cell's class is of the desired custom type
data source and delegated are connected to the View Controller
Each of the 5 buttons/labels is connected to its respective property in the custom class
Dynamic Prototypes is selected
I am able to change the background color of the cell and populate the textLabel.text -- it's just my UILabels that aren't filled.
Here is CellForRowAtIndexPath. It's the exact same method as my other TableVC, which works. Also, I don't need to actually reuse this custom cell, should I be using a different method?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifierProfile = #"profilecell";
ProfileSection0TableViewCell *profilecell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierProfile];
if (profilecell == nil) {
profilecell = [[ProfileSection0TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifierProfile];
}
if (indexPath.section == 0) {
[profilecell setBackgroundColor:[UIColor colorWithRed:54/255.0f green:61/255.0f blue:147/255.0f alpha:1.0f]];
if (indexPath.row == 0) {
profilecell.labelName.text = _userProfile.fullName;
NSString *test = [NSString stringWithFormat:#"%#", profilecell.labelName.text];
NSLog(#"text %#", test); //shows that label is (null)
profilecell.labelName.textColor = [UIColor whiteColor];
//profilecell.labelSpecialty.text = _userProfile.specialty;
//profilecell.labelSpecialty.textColor = [UIColor whiteColor];
}
}
return profilecell;
}
Thanks so much in advance.
.h file:
#interface ProfileSection0TableViewCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel* labelName;
#property (nonatomic, weak) IBOutlet UILabel* labelSpecialty;
#property (nonatomic, weak ) IBOutlet UILabel* labelCurrentRole;
#property (nonatomic, weak) IBOutlet UILabel* labelPosition;
#property (nonatomic, weak) IBOutlet UIButton* facePicture;
#end
.m file:
#implementation ProfileSection0TableViewCell
#synthesize labelName, labelSpecialty, labelPosition, facePicture, labelCurrentRole;
#end
EDIT:
It looks like ProfileSection0TableViewCell is allocated properly, but the member vars inside the cell are nil. And in outlets, here are the connections:
facePicture - Button
labelName- UILabel "person"
labelPosition - UILabel "position"
List item - UILabel "specialty"
labelCurrentRole - UILabel titled school

Either _userProfile.fullName is nil or profilecell.labelName is nil
If you made the cell in interface builder, did you hook up the outlet to labelName correctly?

Related

Customized UITableViewCell reuse with childviewcontroller issue

I have create a UITableView With a Customized TableViewCell,
#import <UIKit/UIKit.h>
#import "Equipment.h"
#import "MCStepperViewController.h"
#class EquipmentCell;
#protocol EquipmentCellDelegate <NSObject>
- (void)EquipmentCell:(EquipmentCell *)cell didChangeStepperValue:(int)stepperValue indexPath:(NSIndexPath*)indexPath;
#end
#interface EquipmentCell : UITableViewCell <MCStepperDelegate>
#property (nonatomic, strong) UIImageView* eqiupmentImage;
#property (nonatomic, strong) UILabel* Title;
#property (nonatomic, strong) UILabel* details;
#property (nonatomic, strong) UILabel* prices;
#property (nonatomic, strong) MCStepperViewController* stepper;
#property (nonatomic, weak) id <EquipmentCellDelegate> delegate;
#property (nonatomic, assign) NSIndexPath* indexPath;
-(void)setContent:(Equipment*)equip;
-(void)setAmount:(int)eq_amount;
#end
The customized cell owns a stepperview controller, and I add the stepperViewController.view to the content view of the cell.
The stepperViewController has a textField and two buttons as strong properties.
The confusing part is, when I scroll the table view, the table cells are ready for reuse process.
The property views of the cell works well, but the view of stepperViewController, which is the subview of the contentView, is becoming duplicate.
Although I have set cell.stepper.view.textField to the right value, but it still doesn't work.
Is there any good solution? I don't think disable reusability is a good attempt.
Thank you very much!
EDIT:
The cellForRowAtIndexPath method:
else if (tableView == _subTable)
{
EquipmentCell *cell = [tableView dequeueReusableCellWithIdentifier:#"detailTable" forIndexPath:indexPath ];
if(!cell)
{
cell = [[EquipmentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"detailTable"];
}
cell.indexPath = indexPath;
Equipment* item = [[[_goodsList objectAtIndex:indexPath.section] objectForKey:#"data"] objectAtIndex:indexPath.row];
cell.delegate = self;
[cell setContent:item];
if (countDic) {
if ([countDic objectForKey:[NSString stringWithFormat:#"%lu", (indexPath.section*100)+(indexPath.row)]]) {
[cell setAmount:(int)[[countDic objectForKey:[NSString stringWithFormat:#"%lu", (indexPath.section*100)+(indexPath.row)]] integerValue]];
}
else
{
[cell setAmount:0];
}
}
return cell;
}
I tried to assign the textfield.text in cell class, but is doesn't work.
- (void)setAmount:(int)eq_amount
{
_stepper.amount = eq_amount;
_stepper.amountText.text = [NSString stringWithFormat:#"%d", eq_amount];
if (eq_amount == self.stepper.min) {
_stepper.minusButton.hidden = YES;
_stepper.amountText.hidden = YES;
}
}
Check where you add your stepperViewController.view to the content view of the cell.
You want to support view recycling for your tableview cell. So as soon as your cell1 is not visible anymore and data 2 needs a cell (cellForRowAtIndexPath gets called), this reused cell will be cell 1. Instead of data1 cell1 now displays data2. If you add the stepperViewController.view in the cellForRowAtIndexPath function, there will be the view added for data1. now if cell1 isn't visible anymore and should be used for data2 you should check if you add the stepperViewController.view again.
Instead of simply adding the view, you could check if there is already a stepperViewController.view in you cell's subviews and remove or reuse it.

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

Is there any way to instantiate a prototype UITableViewCell from a storyboard for testing?

I'm using a combination of XCTest and OCMock for testing an iOS application. I'm using a UITableViewController to present a series of prototype cells which I would like to write some tests for. The cell itself is in a storyboard so I don't believe I can instantiate it from a nib.
Is the only option to use the viewController which uses the cell to instantiate it?
The custom cell class I'm using has a number of 'IBOutlets' connected to the prototype on the storyboard. The cell class looks something like this:
#interface QRFeedAdCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *mainImageView;
#property (strong, nonatomic) IBOutlet UIImageView *blurredImageView;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UIButton *someButton;
#property (strong, nonatomic) IBOutlet UILabel *someLabel;
#property (strong, nonatomic) IBOutlet UILabel *anotherLabel;
#property (nonatomic) BOOL isBusy;
#end
I've tried instantiating the cell using [[QRFeedAdCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"feedAdCell"]; but that will load the cell with all of the properties set to nil.
I've also tried registering the cell, but the tests also fail:
id mockTableView = [OCMockObject niceMockForClass:[UITableView class]];
[mockTableView registerClass:[QRFeedAdCell class] forCellReuseIdentifier:#"feedAdCell"];
QRFeedAdCell *cell = [mockTableView dequeueReusableCellWithIdentifier:#"feedAdCell"];
XCTAssertNotNil(cell, #"");
XCTAssertNotNil(cell.mainImageView, #"");
You can't.
The only way to test it is to instantiate the storyboard, then the view controller, then the cell itself, and then test the properties you set in the Interface Builder.
You can instantiate programmatically but first you need to register it by calling
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"cellId"];
Then you can do custom styling to the cell or whatever you usually do in the storyboard like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellId" forIndexPath:indexPath];
// create and add a label
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 155, 21)];
label.text = #"text";
[cell addSubview:label];
...
}
This is working for me in a basic way. I havent gotten viewwillappear to run yet tho.
ViewdidLoad runs when you call .view on _vc.
#property (nonatomic, strong) Edit_ProfileVC *vc;
#property (nonatomic, strong) UIView *view;
#property (nonatomic) NSMutableDictionary *params;
UIStoryboard *mainSB = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
_vc = [mainSB instantiateViewControllerWithIdentifier:#"EditProfileVC"];
_vc.userData = [[EditUserTableDataSource alloc] initDataSourceWithJSON:self.params];
_view = _vc.view;
XCTAssertNotNil(_view, #"the view is not loading properly");

Resources