Why is my custom UITableViewCell not showing? - ios

I've done custom UITableViewCells before without issue.. but I can't figure out what is going on with my current project.
Here's what I've done...
Create CustomCell.h (subclassing UITableViewCell)
Create an empty XIB and drag in UITableViewCell. Set background to black.
Change class of UITableViewCell in Interface Builder to "CustomCell"
Import CustomCell.h in my DetailViewController
Modify tableView:cellForRowAtIndexPath:
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#"DO I GET HERE?");
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
I would expect my tableview cells to show as black? But they are still showing as white... any ideas? Thanks!
Update: Ok, turns out, it is loading the custom cell... I added a UILabel with some white text. I couldn't see it, but when I highlighted the cell I could see the text was there. So now the question becomes, why is the cell ignoring the black background I have set for the cell?

EDIT: As for why it's not black - I expect there is something obscuring your black - the likeliest candidate for this is the label background being white and not clear.
As well as point 3.
The attributes inspector (the 4th tab) needs to have the reuse identifier set to the identifier you are going to reuse (you use #"cell" in your question). I would try and use something a bit more specific - after all in some apps you might have many types of custom cells.
I think you also need to cast the topLevelObjects to (CustomCell*) thus
if (cell == nil) {
NSLog(#"DO I GET HERE?");
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
cell = (CustomCell*)[topLevelObjects objectAtIndex:0];
}

Seems to ignore the background colour I've set, so I just add a UIView to it with a background colour and that seems to work..

You have to register your custom cell with the tableview
This needs to happen before that delegate is called:
[self.tableView registerClass: [CustomCell class] forCellReuseIdentifier:#"CellIdentifier"];

Related

On scroll UITableViewCell added button image issue

I have UITableViewCell and added button left side of cell for check mark cell is selected or unselected.
Number of rows in uitableview 50.
Issue:
First row is selected and added the checkmark to button, then I scroll the UITableView, I found that another check mark is added to another cell.
Is any one face same issue, your input will be appreciated.
//Cell For Row IndexPath
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:TableCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:UITableViewCell owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.checkButton.tag = indexPath.row;
// Image changes in Storyboard.
-(void)checkButtonAction:(id)sender
{
if ([sender isSelected]) {
[sender setSelected:NO];
}
else {
[sender setSelected:YES];
}
}
Reading your question , its the cell being re-used by iOS sir. The line below is the culprit.
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:TableCell];
What this does is look for an existing cell in the table view, and loads that cell.
You said, You clicked one cell and it had the checkmark, right?
Well, fortunately when you scroll away from it, the cell gets reused by the tableview by above call and is seen again in the list of visible cell with the checkmark.
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:TableCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:UITableViewCell owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.checkButton.tag = indexPath.row;
Looking at your calls in the above method, you never set the checkmark to either on or off, sir. You are reusing the cell that already had the checkmark. So, when you see it again, it also has the checkmark, because you never changed it anyway.
NOTE: IT'S NOT RANDOMLY GIVING CHECKMARKS.
AT THE BEGINNING, DON'T SCROLL. GIVE CHECKMARK TO ALL THE CELLS AND YOU WOULD GET YOUR ANSWER. NOW START SCROLLING. Every cells should have checkmarks now.
Solution:
To solve this issue, in your datamodel, add a isCheckmarkSelected:Bool property.
And, anytime user checks the cell, make isCheckmarkSelected to true.
In the method call, [ cellForRowAtIndexPath ]... read the isCheckmarkSelected value and set the checkmark of the cell.
This would solve the not so random checkmarks issue.
Kind Regards,
Suman Adhikari
Click Here
This above link is worked fine for my question.
in button action add extra code [[self.tableview] reloadData];

Scrolling tableview getting duplicate images

Im using NYXProgressiveImageView(https://github.com/Nyx0uf/NYXImagesKit) to load the image into cell progressively
When I’m scrolling getting duplicate images
How can i avoid this
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:#"Customcell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" 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.Imageview1 loadImageAtURL:[NSURL URLWithString:MyURL]];//Here Imageview1 is NYXProgressiveImageView.
}
I think Its due to progressive download,I want to do similar to this http://nghiatran.me/advanced-issues-the-right-way-to-load-content-in-backgrounds-thread-with-tableview/
How to do this this with NYXProgressiveImageView class
Thank you..
You need to subclass UITableViewCell override prepareForReuse and set the imageView.image = nil. The cell's are reused in a table so unless you clean it out before it gets reused it will appear to "duplicate" your data.
You might give DFImageManager a try, it supports progressive image decoding and there are no known bugs. Check out demo project for an example on how to enable and use progressive decoding.

Changing background colour of UIView cell component to green does not render component in green,

This question is about rendering components of a user interface in Objective-C.
I am trying to get a green stripe to be displayed down the right hand side of a table cell whenever the data in it is considered valid.
In order to achieve this, I decided to create a UIView object, called validatedBar as a component of a custom UITableViewController object in the position and the shape of a thin green bar aligned along the right hand side of the table cell. When the table cell is being rendered to the display, I check to see whether the validatedBar needs to have its background colour set to green as opposed to the regular white. The following extract hopefully shows the relevant information:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *scheduleItem = #"ScheduleItemTableCell";
NSLog(#"cellForRowAtIndexPath");
ScheduleItemTableCell *cell = (ScheduleItemTableCell *)[tableView dequeueReusableCellWithIdentifier:scheduleItem];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ScheduleItemTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
ScheduleListItem *item = [self.scheduleItems objectAtIndex:indexPath.row];
// Other detail being loaded has been removed for brevity.
if(item.validated == YES){
NSLog(#"Should set the validated bar for list item number %ld",(long)indexPath.row);
cell.validatedBar.backgroundColor = [UIColor greenColor];
}
return cell;
}
When the cell is rendered, a green bar does not appear where I think it should even though I see the output displayed in the log.
What am I not doing? Is there anything not clear in this question that I should provide, or is there a pretty obvious solution?
I don't think this is correct...
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ScheduleItemTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
Without looking I don't think you can call LoadNibNamed at this point. It may work for you but it's definitely not by design and more by accident and thus, your view is not working properly and the background colour is not being changed as expected.
Try this approach...
NSString *CellIdentifier = #"ScheduleItemTableCell";
ScheduleItemTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ScheduleItemTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Can you see the difference in how the two code snippets?
Change this and the rest of your code should work. Let me know??
I have the feeling your layout might be wrong. To verify position the bar at the left of the cell (always visible). If that works, then fix your layout. Maybe you set the wrong constraint(s). And please also take the advice of #latenitecoder!
The reason you can't get it to change is because it has already been drawn during the drawRect stage of the view initialisation. That is why when you change orientation and it re-draws itself, you see your colour changed.
To make an immediate change, you will have to re-draw the UIView after setting the colour, as follows:
[cell.validatedBar setNeedsDisplay]
Note that this will not necessarily happen immediately after, but it notifies the view to re-draw as soon as possible. Apologies if the Obj-C code is wrong, I'm a Swiftie.

Can't set properties in Custom UITableViewCell

I made a custom table view cell - i have a header, implementation, and nib. In the nib I set the style to custom, dragged a label on it and made an outlet in the nibs file owner.
From my UITableView Controller I have this code:
static NSString *CellIdentifier = #"adbActivityCell";
adbActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
//cell =[[adbActivityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.price.text = [NSString stringWithFormat:#"cell #%d", indexPath.item + 1];
return cell;
If I run this as is XCode tells me that the UITableView Controller is not key value compliant for the label property (the label is named "price"). If I comment out the two lines above and uncomment that one line my application runs, but the label doesn't show up at all, even if I set default text for it.
I've spent quite a lot of time researching tutorials and questions on here with no luck.
Its all about view hierarchy.
You have to add your label outlet to the custom UITableViewCell, because it is the superView of your label in view heirarchy.
That means label is contained in custom cell thats why you have add outlet to custom cell.
self.view->tableView->CustomCellView->UILabel
In your customCell.h file set the IBOutlet of the label. Your problem will be solved.

Issue with UITextField values in custom cell while scrolling fast ios

I have a simple table with custom cells each containing a textfield. In cellForRowAtIndexPath: I create and initialize each cell depending on indexPath.row:
case 0:
{
CellIdentifier = #"TextEditCell";
TextEditCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
[cell configureCellWithText: [self.valueArray objectAtIndex:0]
placeholder: #"value no.0"]
[cell performAction: #selector(saveValue0:)
forControlEvent: UIControlEventEditingDidEnd
inTarget: self];
return cell;
}
configureCellWithText:placeholder: sets text and placeholder of cell's textField.
performAction:forControlEvent:inTarget refers directly to textField and saves the value of textField to local array to be accurate when used again.
Problem occurs, when I scroll the table fast. Values from different cells copy to another cells and modify local array. I can't find out why it happens. Anyone have any idea? I can provide more code if needed.
This is happening because you are reusing the cells and configureCellWithText is being run after the cell is reused. To solve this you could:
Don't reuse cells - But this would really hurt your performance.
If you are running on 6.0 you can use tableView:didEndDisplayingCell:forRowAtIndexPath: to cancel the text setting action when the cell scrolls off screen.
You can create a flag in your custom cell class that you set when you dequeue a cell.
Edit
Because I do not know how your cell works. It is hard for me to give you anything more then a sudo code concept.
Here is my sudo code:
Tableview Cell for row...
- dequeue cell
- [cell cancel_previous_action]
- set new actions.

Resources