Hello I'm having a little bit of trouble with something I'm working on. I have a static TableView with sections and I'm trying to get my text to be positioned in a certain spot so I have a ui label linked to a uitableviewcell. But for some reason It's not working. If you have any ideas on why I'm having trouble with this that would be great. I'm also a bit new to iOS development, just putting that out there.
TableViewController.m:
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
int row = [indexPath row];
if (indexPath.section==0)
cell.textLabel.text = _1[row];
if (indexPath.section==1)
cell.textLabel.text = _2[row];
if (indexPath.section==2)
cell.textLabel.text = _3[row];
if (indexPath.section==3)
cell.textLabel.text = _4[row];
if (indexPath.section==4)
cell.textLabel.text = _5[row];
if (indexPath.section==5)
cell.textLabel.text = _6[row];
if (indexPath.section==6)
cell.textLabel.text = _7[row];
if (indexPath.section==7)
cell.textLabel.text = _8[row];
return cell;
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
#end
TableCell.h:
#import <UIKit/UIKit.h>
#interface TableCell : UITableViewCell
#property (strong,nonatomic) IBOutlet UILabel *TitleLabel;
#end
replace this line
cell.textLabel.text = _1[row];
with
cell.TitleLabel.text = _1[row];
this line is not needed anymore
if (cell == nil) {
cell = [[TableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
in case TableCell is with xib, then you need to load it.
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TableCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
cell.TitleLabel.text = _1[row];
return cell;
you should check this
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier
method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically. if you previously registered a class or nib file with the same reuse identifier, the class you specify in the cellClass parameter replaces the old entry (Apple Document)
in your viewDidLoad method
[self.tableView registerClass:[TableCell class]
forCellReuseIdentifier:#"CellIdentifier"];
Related
I have two UITableView and I want to use the same custom cell for both UITableView.
The problem is only one of them works fine. Only the first table display rows.
When I debug I see the cell.lb_subject is nil for the second table.
The MyCustomCell was defined as prototype cell(Is NOT a xib) only on the first table, because I don't see the point to define the same row twice.
- (UITableViewCell *)tableView:(UITableView *)mytableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"MyCustomCell";
MyCustomCell *cell = [mytableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
MyRowData* data = [tableData objectAtIndex:indexPath.row];
cell.lb_subject.text = data.subject;
I have two UIViewController
One table per each UIViewController
I have not Xib, I have a storyboard.
I've uploaded to my repo a demo
Follow this step.
Your ViewController
ViewController.h
NSMutableArray *tableData;
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
tableData = [[NSMutableArray alloc]initWithObjects:#"test1",#"test2",#"test3",nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Here MyCustomCell is identifier for both tableview cell.
static NSString *simpleTableIdentifier = #"MyCustomCell";
//tblCell is your custom tableview cell.
tblCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[tblCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.lb_subject.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
Your Custom cell
tblCell.h
//Here lb_subject is common Outlet for both label of both cell.
#property (strong, nonatomic) IBOutlet UILabel *lb_subject;
Give Tag to your both tables and then use dequeueReusableCellWithIdentifier for both tables separately.
if (tableView.tag==1) {
static NSString *simpleTableIdentifier = #"MyCustomCell";
MyCustomCell *cell = [mytableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
MyRowData* data = [tableData objectAtIndex:indexPath.row];
cell.lb_subject.text = data.subject;
return cell;
}
else {
static NSString *simpleTableIdentifier = #"MyCustomCell";
MyCustomCell *cell = [mytableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
MyRowData* data = [tableData objectAtIndex:indexPath.row];
cell.lb_subject.text = data.subject;
return cell;
}
I would like to register the cell with the tableView in the class of customCell. something like this
class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell {
let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)
let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell
return cell
}
You can see a more detailed answer Here.
I am guessing that you are having two prototype cell for each of your table.
Let's say for, table A - "CustomCellA" & for table B - "CustomCellB"
both customCell's parent class should be same.
Now in cellForRowAtIndexPath write below code,
MyCustomCell *cell;
if (tableView == tableViewA)
cell = [mytableView dequeueReusableCellWithIdentifier:#"CustomCellA"];
else
cell = [mytableView dequeueReusableCellWithIdentifier:#"CustomCellB"];
Your cell's Outlet and attributes should be find for both tableview.
Do let me know the results.
Thanks, Hope this helps.
You've not added cell in other table
if you do not want to create another prototype cell for viewController 2 then you need to use XIB
Register your XIB :
Register your customcell in ViewDidload instead of cellForRow
[self.tableView registerNib:[UINib nibWithNibName:#"CustomCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"CustomCell"];
and,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = (CustomCell *)[topLevelObjects objectAtIndex:0];
}
cell.cellLabel.text = _arrData[indexPath.row];
return cell;
}
If you still face same problem then let me know
I have a customTableViewCell that I am trying to set an image and some text on. The cell is created successfully, but the properties are nil. I'm not sure if there is something I'm missing in the .xib, but I just can't figure it out. The outlets are set in the .h of the customViewCell and connected in the .xib
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"socialMediaCell";
CustomSocialMediaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
CustomSocialMediaTableViewCell *tagCell = (CustomSocialMediaTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"socialMediaCell"];
if (cell == nil) {
cell = [[CustomSocialMediaTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
tagCell = [[CustomSocialMediaTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:#"socialPosts"];
UIImage* image = [UIImage imageWithData:imageData];
cell.socialMediaTextView.text = #"TEXT";
cell.socialMediaImageView.image = image;
return cell;
}
You are referencing the same CustomTableViewCell twice and then you are allocating things to it:
static NSString *cellIdentifier = #"socialMediaCell";
****Declared once here****
CustomSocialMediaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
****Declared a second time here***
CustomSocialMediaTableViewCell *tagCell = (CustomSocialMediaTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"socialMediaCell"];
Use the first declared cell and remove the second.
Then you are saying if the cell is empty, style it and you have done that twice. Do it once.
if (cell == nil) {
cell = [[CustomSocialMediaTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault];
}
Then you can add and assign the things you need.
I have a feeling that you may have tried to do this a more difficult way by modern methods by creating a .xib for it when you can just select the TableViewCell in your storyboard and set it's class. Then you can ctrl + drag from your UILabel and UIImage to your .h file.
If you do not wish to do this or have not structured it this way ensure that your referenced UILabel and UIImage are connected properly and that you have declared an IBOutlet for both - like so
#property (nonatomic, weak) IBOutlet UILabel * customText;
Edit inside if (cell==nil) with the below code
if (cell == nil) { //Check cell is nil or not
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:#"CustomSocialMediaTableViewCell"
owner:self options:nil]; //if cell is nil add CustomSocialMediaTableViewCell Xib
for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomSocialMediaTableViewCell class]])
cell = (CustomSocialMediaTableViewCell *)oneObject;
}
I have a subclass of UITableViewCell with a xib that I want to use as my table view cell. I need to modify the cell's properties on a cell by cell basis. Here is my code. The NSLog reveals that the properties are changed correctly, but when I run the app, the usernameLabel stays the same as the placeholder in the xib file. I'm grateful for any help and happy holidays.
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableItem";
// ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
LiveCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"LiveCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
if (dataLoaded == YES) {
cell.usernameLabel = [[UILabel alloc] init];
NSLog(#"Data Cell Loaded");
PFObject *liveObject = [tableData objectAtIndex:indexPath.row];
cell.usernameLabel.text= liveObject[#"Name"];
NSLog(#"Name Label: %#", cell.usernameLabel.text);
return cell;
}
else {
return cell;
}
}
Consider this code:
cell.usernameLabel = [[UILabel alloc] init];
NSLog(#"Data Cell Loaded");
PFObject *liveObject = [tableData objectAtIndex:indexPath.row];
cell.usernameLabel.text= liveObject[#"Name"];
NSLog(#"Name Label: %#", cell.usernameLabel.text);
return cell;
You made a label and set its text, but so what? You never put it into the interface of the cell. So the label comes into existence, its text is set, and it then vanishes in a puff of smoke.
If the cell already has a usernameLabel in its interface, then it is wrong to replace it with this new one that is not in the interface. If it doesn't already have a usernameLabel, then you need to put this one in the interface. Either way, this code is silly.
I have a new project where I had to drag a prototype cell to a existing table view. I then
added some labels to the prototype cell with appropriate tags
set a identifier for the cell
and then in my tableview delegate when I get the delegate callback :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"myCustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:11];
I see that nameLabel is nil. I have double checked and tripled checked the tag and reusable identifier with no luck. In the storyboard, I see that the tableview has the pro type cell as its cell with the contentView showing my labels. What am I missing?
Do you ever create a new cell in the if (cell == nil) branch? If yes, you are creating a regular UITAbleViewCell there. Do not expect any custom lable there, because you don't load it from any nib file nor from the storyboard.
Of what type is the cell object? NSLog it or have a look in the debugger which type is actually created.
You are allocating UITableViewCell which don't contain your custom label. If you have created a View using Storyboard then you should allocate that view using following method.
You can create a Utility method to get Class instance from NibName
+ (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass
{
if (nibName && objClass)
{
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName
owner:nil
options:nil];
for (id currentObject in objects )
{
if ([currentObject isKindOfClass:objClass])
return currentObject;
}
}
return nil;
}
Use this in your code like
CustomViewCell *cell = (CustomViewCell *)[tableView dequeueReusableCellWithIdentifier:#"CustomViewCell"];
if (cell == nil)
{
cell = [Utility loadNibNamed:#"CustomViewCell" ofClass:[CustomViewCell class]];
}
cell.yourLabel.text = #"Dummy Text";
Hopefully this will help you.
I have a custom UIView subclass Leaderboard which itself contains a tableview tblLeaderboard. The interface was created with a xib. Additionally, I have a UITableViewCell subclass LeaderboardCell which also has a xib. I'm having trouble registering the cell in the tableview.
Here's what I tried, first I registered the nib with the tableview:
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
leaderInfo = [NSMutableArray array];
tblLeaderboard.dataSource=self;
[tblLeaderboard registerNib:[UINib nibWithNibName:#"LeaderboardCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"leaderboardCell"];
}
return self;
}
Then when initializing the cell I have:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"leaderboardCell";
LeaderboardCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[LeaderboardCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
//ADDED THIS IN CASE DEFAULT CELL WAS LOADED
cell.textLabel.text = [[leaderInfo objectAtIndex:indexPath.row] objectForKey:#"name" ];
cell.name.text = [[leaderInfo objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.score.text = [[[leaderInfo objectAtIndex:indexPath.row] objectForKey:#"score"] stringValue];
return cell;
}
The cell doesn't load the nib. It just makes a custom cell (the default cell loads the name and score properly, so I know the datasource is working fine).
I'm not sure if I'm getting in trouble here for using a UIView and not a ViewController to control my UITableView?
I don't really have alot of experiences with xibs, but in my old project I did it this way:
static NSString *leaderBoardIdentifier = #"leaderboardCell"; //cell identifier same name as identifier in xib cell's attribute inspector
LeaderboardCell *cell = (LeaderboardCell *)[tableView dequeueReusableCellWithIdentifier:leaderBoardIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:leaderBoardIdentifier owner:self options:nil];
cell = [nib objectAtIndex:0];
}
I have my cell's xib file with h/m files. In my .h I've just connected elements from xib.
initWithStyle and setSelected methods in cell class have default code in them.