UIImage is showing on simulator, not on a real device - ios

I have tried all of the suggestions in other similarly titled questions. My case seems to be special. Here's my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView respondsToSelector:#selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if ([menu_List count] == 0){
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSString *newString = [[MainMenuArray objectAtIndex:indexPath.row] stringByReplacingOccurrencesOfString:#" " withString:#""];
UIImage *image1 = [UIImage imageNamed:[NSString stringWithFormat:#"%#%#", newString,#".png"]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image1];
[imageView setFrame:CGRectMake(5.0, 1.0, 41.0, 41.0)];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[cell addSubview:imageView];
cell.textLabel.textColor = [UIColor whiteColor];
GetData *GD = [[GetData alloc] init];
cell.backgroundColor = [GD colorWithHexString:#"18204a"];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [MainMenuArray objectAtIndex:indexPath.row]];
}else{
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%#", [menu_List objectAtIndex:indexPath.row]];
cell.textLabel.textColor = [UIColor whiteColor];
GetData *GD = [[GetData alloc] init];
cell.backgroundColor = [GD colorWithHexString:#"18204a"];
}
return cell;
}
So the string name is correct, and it matches the case. I've checked my build settings, and the images are in copy bundle resources. All show up in the simulator. Only two of the seven appear in the list. If I change the order in which they appear, it is still the same two images that show up. They all show up in the simulator. I am using ECSlidingViewController to create a menu and I want the images to show up. Any idea on what could be causing it?

Clean your project and check for upper case letters. Simulator is not case sensitive, but real devices does not load an image if you use "myimage.png" on your code but your image name is MyImage.png

Make sure your png is enabled for the schema

Related

Modifying one cell in CellForRowAtIndexPath changes multiple cells

I have an array of names that I'm showing via tableview. You can select up to a total of 3 names, and you cannot re-select the same names. To do this I implemented the following code in cellForRowAtIndexPath:. When I run the code the names come up fine, but there are multiple red cells with names that I did not select.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionNames = [names objectForKey:sectionTitle];
NSString *name = [sectionNames objectAtIndex:indexPath.row];
cell.textLabel.text = name;
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3]) {
[cell setUserInteractionEnabled:NO];
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
Reading a similar problem here, they were saying that it's because the cells are being reused - but if this were true, how is the tableview still displaying the correct names in the correct position?
I tried to simplify the code into this, and still to no avail, there were multiple red cells.
myIP = [NSIndexPath indexPathForRow:0 inSection:0];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TableCell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *sectionTitle = [nameSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionNames = [names objectForKey:sectionTitle];
NSString *name = [sectionNames objectAtIndex:indexPath.row];
cell.textLabel.text = name;
if (indexPath == myIP) {
cell.backgroundColor = [UIColor redColor];
}
return cell;
}
I can post a screenshot if needed. Note: The intended names were correctly labeled with red.
The issue is happening due to cell re-using. When a cell with red background is re-used it'll still be in red background, you are not re-setting it anywhere in your code. You need to put a else case to your if condition used in cellForRowAtIndexPath: method.
if ([name isEqualToString: self.name1] || [name isEqualToString: self.name2] || [name isEqualToString: self.name3])
{
[cell setUserInteractionEnabled:NO];
cell.backgroundColor = [UIColor redColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
[cell setUserInteractionEnabled:YES];
cell.backgroundColor = [UIColor clearColor];
// Other stuffs
}

Reloading UITableView causes incorrect setting of labels

When my UITableView loads for the first time, everything in the code below functions correctly. However, if it reloads for whatever reason (refresh, etc.), it starts assigning a cell.bestMatchLabel.text value of #"Best Match" to random cells, rather than only the first one as I specified in the code. Why is calling reload on my table causing the below code to not run correctly?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//load top 3 data
NSDictionary *currentSectionDictionary = _matchCenterArray[indexPath.section];
NSArray *top3ArrayForSection = currentSectionDictionary[#"Top 3"];
// if no results for that item
if (top3ArrayForSection.count-1 < 1) {
// Initialize cell
static NSString *CellIdentifier = #"MatchCenterCell";
EmptyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[EmptyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// title of the item
cell.textLabel.text = #"No items found, but we'll keep a lookout for you!";
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.detailTextLabel.text = [NSString stringWithFormat:#""];
[cell.imageView setImage:[UIImage imageNamed:#""]];
return cell;
}
// if results for that item found
else {
// Initialize cell
static NSString *CellIdentifier = #"MatchCenterCell";
MatchCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[MatchCenterCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
tableView.separatorColor = [UIColor clearColor];
if (indexPath.row == 0) {
cell.bestMatchLabel.text = #"Best Match";
cell.bestMatchLabel.font = [UIFont systemFontOfSize:12];
cell.bestMatchLabel.textColor = [UIColor colorWithRed:0.18 green:0.541 blue:0.902 alpha:1];
[cell.contentView addSubview:cell.bestMatchLabel];
}
// title of the item
cell.textLabel.text = _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Title"];
cell.textLabel.font = [UIFont systemFontOfSize:14];
// price + condition of the item
NSString *price = [NSString stringWithFormat:#"$%#", _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Price"]];
NSString *condition = [NSString stringWithFormat:#"%#", _matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Item Condition"]];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%# - %#", price, condition];
cell.detailTextLabel.textColor = [UIColor colorWithRed:0.384 green:0.722 blue:0.384 alpha:1];
// Load images using background thread to avoid the laggy tableView
[cell.imageView setImage:[UIImage imageNamed:#"Placeholder.png"]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// Download or get images here
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][#"Top 3"][indexPath.row+1][#"Image URL"]]];
// Use main thread to update the view. View changes are always handled through main thread
dispatch_async(dispatch_get_main_queue(), ^{
// Refresh image view here
[cell.imageView setImage:[UIImage imageWithData:imageData]];
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 2.5;
[cell setNeedsLayout];
});
});
return cell;
}
}
That is because table dequeue same cell for multiple indexes as you scroll down, so you need to add else statement in the following code
if (indexPath.row == 0) {
cell.bestMatchLabel.text = #"Best Match";
cell.bestMatchLabel.font = [UIFont systemFontOfSize:12];
cell.bestMatchLabel.textColor = [UIColor colorWithRed:0.18 green:0.541 blue:0.902 alpha:1];
[cell.contentView addSubview:cell.bestMatchLabel];
[cell.bestMatchLabel setHidden:NO];
} else {
[cell.bestMatchLabel setHidden:YES];
}
but a better approach for this case is to use different cell identifier for that row and only add the bestMatchLabel once when first creating the cell

property not found on object UiTableViewCell

i'm trying to show fetch data from core data into my tableview the problem is i get the.
´Property yttitle not found on object of type UITableView´
i've made sure the cell identifier is correct and the yttitle and the other are synthesized correctly. Why am i getting this error?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
// Configure the cell...
NSManagedObject *device = [devices objectAtIndex:indexPath.row];
cell.ytTitle.text = [NSString stringWithFormat:#"%#", [device valueForKey: #"songName"]];
cell.ytAuthor.text = [NSString stringWithFormat:#"%#", [device valueForKey: #"author"]];
[cell.ytThumbnail.layer setBorderColor: [[UIColor grayColor] CGColor]];
[cell.ytThumbnail.layer setBorderWidth: 1.0];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:#"%#", [device valueForKey: #"link"]]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
cell.ytThumbnail.image = image;
return cell;
}
UITableViewCell doesn't have that properties, you have to create your own custom cell view and load it, you can use this code to load it from an xib
NSArray *xibObj = [[NSBundle mainBundle] loadNibNamed:#"CustomTVCell" owner:nil options:nil];
for(id currentObj in xibObj){
if ([currentObj isKindOfClass:[CustomTVCell class]]) {
cell = (CustomTVCell *) currentObj;
}
}
Probably If I understand what you want to do, creating a custom cell is not necessary. You can simply use the UITableViewCell with UITableViewCellStyleSubtitle and use the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
// Configure the cell...
NSManagedObject *device = [devices objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [device valueForKey: #"songName"]];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#", [device valueForKey: #"author"]];
[cell.imageView.layer setBorderColor: [[UIColor grayColor] CGColor]];
[cell.imageView.layer setBorderWidth: 1.0];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:#"%#", [device valueForKey: #"link"]]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
cell.imageView.image = image;
return cell;
}
It's complaining because you try to call this property (yttitle) on UITableViewCell object and there isn't any property like that. You should cast UITableViewCell to your custom class like that:
YOURCUSTOMTableViewCell *cell = (YOURCUSTOMTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) cell = [[YOURCUSTOMTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
And make sure your YOURCUSTOMTableViewCell contain the property (yttitle, etc).
Hope this help.
Are you using custom cell for this TableView? Note that your method is return and using standard UITableViewCell, and this standard cell don't have a #propoerties like ytTitle and ytAuthor.
So if you're using custom cell you have to change it in three places in your code:
- (MyCustomCellClass *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCellClass *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) cell = [[MyCustomCellClass alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
...
If you declare properties in your view controller (this don't make sense) you should move it to custom cell and get rid of it in VC.

UITableView set checkbox state from database values

I am storing checkbox values (true/false) in database. I want to set checkbox checked/unchecked depending on the values stored in database. how to do that ? my code is,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%i",indexPath.row];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.frame=CGRectMake(75.0, 50.0, 150.0, 20.0);
cell.textLabel.text=[listArray objectAtIndex:indexPath.row];
NSLog(#"Checked arr size %i",[checkedArr count]);
BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue];
UIImage *image = (checked) ? [UIImage imageNamed:#"white_bg.png"] : [UIImage imageNamed:#"tick.png"];
cell.accessoryView = [[UIImageView alloc] initWithImage:image];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue];
[checkedArr removeObjectAtIndex:indexPath.row];
[checkedArr insertObject:(checked) ? #"FALSE":#"TRUE" atIndex:indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImage *newImage = (checked) ? [UIImage imageNamed:#"tick.png"] : [UIImage imageNamed:#"white_bg.png"];
cell.accessoryView=[[UIImageView alloc] initWithImage:newImage];
NSLog(#"Val is %i",val);
NSLog(#"selected is %#",[listArray objectAtIndex:indexPath.row]);
}
I am storing those values in database using an array checkedArr.
[dbObj insertQuestData:Question answers:listArray checkVal:checkedArr];
here dbObj is database object, others are values that i am storing to database.
Please let me know how to achieve the same.
The list array and checked array should be mutable array sent to database function which fills up the data.
self.listArray = [NSMutableArray array];
self.checkedArray = [NSMutableArray array];
[dbObj insertQuestData:Question answers:self.listArray checkVal:self.checkedArray];
// [self insertList:self.listArray andChecks:self.checkedArray];
The insertQuestData methods should fill up data like this -
- (void) insertList:(NSMutableArray *)list andChecks:(NSMutableArray *)checks
{
for (int i = 0; i < 100; i++) {
[list addObject:[NSString stringWithFormat:#"item %d", i]];
BOOL checkValue = (i % 4) == 0 ? YES : NO;
[checks addObject:[NSNumber numberWithBool:checkValue]];
}
}
The table datasource methods should be like this - (notice the commented line as correction to your code)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"CellIdentifier";//[NSString stringWithFormat:#"Cell%i",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0];
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18.0];
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// cell.textLabel.frame=CGRectMake(75.0, 50.0, 150.0, 20.0);
cell.textLabel.text=[self.listArray objectAtIndex:indexPath.row];
NSLog(#"Checked arr size %i",[self.checkedArray count]);
BOOL checked = [[self.checkedArray objectAtIndex:indexPath.row] boolValue];
UIImage *image = (!checked) ? [UIImage imageNamed:#"white_bg.png"] : [UIImage imageNamed:#"tick.png"];
cell.accessoryView = [[UIImageView alloc] initWithImage:image];
return cell;
}
This is what my sample code show on iPhone - [every 4th row is (green)ticked/selected and others are grey/unselected]
Feel free to ask if you have any specific questions / issues you face.

ios6, UITableViewCell background

I have a trouble with ios6 and UITableViewCell. The backgroundcolor is not set, this is what it used to work in ios5
-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
NSString *contentForThisRow = [[self myArray] objectAtIndex:[indexPath row]];
cell = [tableView dequeueReusableCellWithIdentifier:#"noticeCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"noticeCell"];
}
cell.textLabel.text = contentForThisRow;
id path = [NSString stringWithFormat:#"myip%#.jpeg", IDimage];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
cell.imageView.image = img;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:15.0];
UIColor *cellBackground = [UIColor colorWithRed:235.0/255.0 green:245.0/255.0 blue:255.0/255.0 alpha:0.25];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell setBackgroundColor:cellBackground];
return cell;
}
but now I'm not able to set it again!
How should I do this?
Thanks!
Use
cell.contentView.backgroundColor= [UIColor blueColor];
you may use clear color for cell textlabel background.
It should work.
:)

Resources