UIImage doesn't show unless i click on my table cell - xcode

I've spent some time writing an xcode application today. I'm using a UITableView with a seperate xib for the table view cell. All is working well besides one quirky thing. In my code I set my image on the table cell xib from an array. When i run the app I've found out that the image does not appear on the table cell until I click on the table cell. And when I click on a different cell, the image on my previously selected cell disappears.
Funny thing is that I set my label on the table cell xib exactly the same way however I don't get the issue with the label.
Here is my code. I would greatly appreciate some help.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;
cell = [tbHealthCheck dequeueReusableCellWithIdentifier:#"CONTENT"]; //all the cells in this table are named content (identifier for all cells in table)
if (cell == nil){
[[NSBundle mainBundle] loadNibNamed:#"FirstViewTableCell" owner:self options:nil];
cell = tcHealthCheck;
tcHealthCheck = nil;
}
UILabel *l1 = (UILabel *) [cell viewWithTag:0];
[l1 setText:[healthCheckCategory objectAtIndex:indexPath.row]];
l1.font = [UIFont fontWithName:#"arial" size:14];
[l1 setTextColor:[UIColor blackColor]];
UIImageView *img1 = (UIImageView *) [cell viewWithTag:1];
NSString *status = [healthCheckStatus objectAtIndex:indexPath.row];
if ([status isEqualToString:#"RED"])
{
img1.image = [UIImage imageNamed:#"red-light-badge.png"];
}
else if ([status isEqualToString:#"YELLOW"])
{
img1.image = [UIImage imageNamed:#"yellow-light-badge.png"];
}
else if ([status isEqualToString:#"GREEN"])
{
img1.image = [UIImage imageNamed:#"green-light-badge.png"];
}
return cell;
}

Related

TableView selection looks weird if I remove border

I have a very standard NSTableView (this is Mac, not iOS) with a few text rows. Each row is an NSTextField.
I was unhappy with the border of the text fields (see image) so I removed it. However doing so the selected cells look weird.
How can I fix that?
before:
after
Note:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSTextField *cell = [tableView makeViewWithIdentifier:#"MyView" owner:self];
if (cell == nil) {
cell = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, tableView.bounds.size.width, 30)];
cell.identifier = #"MyView";
cell.editable = NO;
cell.bordered = NO;
}
cell.stringValue = [NSString stringWithFormat:#"row %d", row];
return cell;
}
I have tried to set cell.selectable = YES but it didn't help. I have also tried to play with some properties on the storyboard ("focus ring", ...) but it didn't work either. Any idea?
Setting cell.backgroundColor = [NSColor clearColor]; did the trick (transparent background)

xcode Converting UITableView to UICollectionView (no valid cell)

EDIT: I should specify that this only happens when I attempt to use the UICollectionViewFlowLayout, not when I try to use a custom view. But either way nothing ever shows up on the CollectionView though it was working just fine before I converted from a TableView.)
So I've been trying to convert a UITableView that I had into a UICollectionView. So far so good. But when I try to run the app it gives me the error:
'the collection view's data source did not return a valid cell from -collectionView:cellForItemAtIndexPath: for index path {length = 2, path = 0 - 0}'
I checked all the similar questions and answers here... so in my viewDidLoad I have (tableView is actually a UICollectionView):
UINib * placeCell = [UINib nibWithNibName:#"Shops" bundle:nil];
[self.tableView registerNib:placeCell
forCellWithReuseIdentifier:CellIdentifier];
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UICollectionView *)tableView numberOfItemsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_entries count];
//return 5;
}
- (void)tableView:(UICollectionView *)tableView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.item == [_entries count]-1 && page > 1) {
NSLog(#"load more");
//add footer view loading
if (c_page == page) {
// _tableView.tableFooterView = nil;
}
else
{
c_page++;
[self loadPlace:c_page];
}
}
}
- (UICollectionViewCell *)tableView:(UICollectionView *)tableView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
PlaceCell *cell = (PlaceCell *)[tableView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
UINib * placeCell = [UINib nibWithNibName:#"Shops" bundle:nil];
//cell = [cellLoader instantiateWithOwner:self options:nil];
NSArray *topLevelItems = [placeCell instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
Place *p = [_entries objectAtIndex:indexPath.row];
cell.placeName.text = p.PName;
NSLog (#"p:%#", p.PName")
cell.placeImg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:p.PImage]]];
return cell;
}
I went into the xib of the UICollectionViewCell (PlaceCell) and made sure that "Cell" was the reuseidentifier. And I made sure that the datasource and delegate were connected to file's owner in the collectionView.
I also noticed that when I use a custom layout instead of the flow layout (like this one: https://github.com/ShadoFlameX/PhotoCollectionView/blob/master/CollectionViewTutorial/BHPhotoAlbumLayout.m ) it doesn't give me that error... but my collectionview still isn't populated.
So I'm wondering if there's some sort of log I can run or something I can do to figure out what's going wrong. Because I've tried all the solutions I've seen and it hasn't gotten me anywhere.
When you make a cell in a xib file you should register the xib, not the class. Also, when you register either the class or xib (or make the cell in the storyboard), you don't need an if (cell==nil) clause because your cell will never be nil when you dequeue it with dequeueReusableCellWithReuseIdentifier:forIndexPath:. You should delete that clause.
So the problem is: "Switched from UITableView to UICollectionView and no valid cell is being returned." It is really a two part answer. The crux of which is every instance of UITableView...
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height-50)];
...you want to turn into "CollectionView"
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height-50)];
Everything that's a "row":
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
...you'll want to turn into an "item."
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
Ultimately I had to delete the following section entirely:
UINib * placeCell = [UINib nibWithNibName:#"Shops" bundle:nil];
//cell = [cellLoader instantiateWithOwner:self options:nil];
NSArray *topLevelItems = [placeCell instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
My best guess is that the Nib was being loaded twice and that Xcode was complaining that the data wasn't being loaded by the original. So getting rid of that second entry got my cells loaded and populated with data. Hope this helps someone.

Table view not working smoothly with SDWebImage

Why isn’t my table view not working smoothly? I use SDWebImage to set the image from a URL. I'm trying to draw 3 images in each row of the table view.
It works fine when arrList is small, but when I increase arrList, it becomes slower and slower (look like a little vibration when I scroll the table view).
This is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
CustomCellTable *cell = (CustomCellTable*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray * nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCellTable" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell setBackgroundColor:brgColor];
// item 1
if ((indexPath.row*3) > arrList.count-1) return cell;
vod *Item = [arrList objectAtIndex:(indexPath.row*3)];
[cell.image1 setHidden:NO];
[cell.image1 setImageWithURL:[NSURL URLWithString:Item.imageURl] placeholderImage:[UIImage imageNamed:#"poster_default.png"]];
[cell makeRating:[Item.rating floatValue] positionOf:14];
// item 2
if ((indexPath.row*3 +1) > arrList.count-1) return cell;
Item = [arrList objectAtIndex:(indexPath.row*3+1)];
[cell.image2 setHidden:NO];
[cell.image2 setImageWithURL:[NSURL URLWithString:Item.imageURl] placeholderImage:[UIImage imageNamed:#"poster_default.png"]];
[cell makeRating:[Item.rating floatValue] positionOf:118];
// item 3
if ((indexPath.row*3 +2) > arrList.count-1) return cell;
Item = [arrList objectAtIndex:(indexPath.row*3+2)];
[cell.image3 setHidden:NO];
[cell.image3 setImageWithURL:[NSURL URLWithString:Item.imageURl] placeholderImage:[UIImage imageNamed:#"poster_default.png"]];
[cell makeRating:[Item.rating floatValue] positionOf:225];
return cell;
}
Look into down sampling the images so you're not storing/loading the large versions of the images while scrolling.
https://github.com/toptierlabs/ImageCacheResize
This library combines SDWebImage with UIImage+Resize. Using it instead of just SDWebImage might help. Specifically use the following method to load the images:
- (void)setImageWithURL:(NSURL *)url andResize:(CGSize)size withContentMode:(UIViewContentMode)mode;

Custom UITableViewCell Content Showing Up Outside of Grouped Style Content View

I am having this issue where my custom table view cell isn't indenting its content when being shown in a grouped style.
static NSString *CellIdentifier = #"GameListCell";
GameViewCell *cell = (GameViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"GameViewCell" owner:self options:nil];
for (UIView *view in views)
{
if ([view isKindOfClass:[GameViewCell class]])
{
cell = (GameViewCell *)view;
}
}
}
Board *game = (Board *)[boardArray objectAtIndex:indexPath.row];
cell.opponentName.text = [NSString stringWithFormat:#"Game vs %#",game.opponentName];
cell.gameDescription.text = [NSString stringWithFormat:#"Last Move: %#",game.opponentName];
return cell;
Does anyone have any clue what might be going on?
I can't post an image yet, but basically the content is too far left and is outside of the rounded rectangle of the cell.
I'm thinking it might have something to do with the Horizontal Space Constraint in the XIB file for the table view cell.. It has a value of "Constant" and "20". But how do I make the stuff show up in the Content View of the cell?
It looks fine in the "Plain" style, but doesn't indent for grouped style.
Thanks!

xcode uitableview cell image align

I'm sure this is very simple but i can't find any documentation about it..
in my uitableview i have the text labels aligned to the right (my app is in heberw which is rtl)
i'm trying to add an image to the cell to the RIGHT of the text label, no luck, so far the image is aligned to the left and i can't find a way to align it to the right side of the cell.
here's my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.imageView.image = [UIImage imageNamed:#"tableicon.png"];
NSString *cellValue = [[stories objectAtIndex:indexPath.row] objectForKey:#"ArticleTitle"];
cell.textLabel.text = cellValue;
cell.textLabel.textAlignment = UITextAlignmentRight;
cell.textLabel.font = [UIFont systemFontOfSize:17];
cell.textLabel.numberOfLines = 2;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
Instead of adding the image as a subview of your UITableViewCell, add a container with right aligned subviews.
UIImageView *pic = myImageView;
UIView *picContainer = [[UIView alloc] initWithFrame:cell.frame];
picContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
picContainer.contentMode = UIViewContentModeRight;
pic.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
pic.center = CGPointMake(picContainer.frame.size.width-70, picContainer.frame.size.height/2);
[picContainer addSubview:pic];
[cell.contentView addSubview:picContainer];
This has the added benefit of automatically readjusting when the orientation changes.
try this one
cell.accessoryView=[UIImage imageNamed:#"tableicon.png"];

Resources