Trying to add an UIImage into default UITableViewCell. The image was appeared, but part of separator line (under the image) became disappeared. How can I fix that?
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"text";
cell.imageView.image = [UIImage imageNamed:#"map.png"];
That's how it looks like now - screenshot
I suggest you add this line of code after you instantiate de UITableView:
[_tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
If this doesn't work, I suggest that you execute the app on a real device. I have had a similar problem some days ago, and the problem was with the iOS simulator...
I hope this helps!
Related
With iOS 8 I have come up with some issues using UITableView. In fact, I have a NavigationController and its RootController set as my ViewController with UITableView. Then I configure my table, add cells and set their style to UITableViewCellStyleValue1 and nothing happens at the time it should display a detailed text label to the right of the textLabel. I suppose that there is some missing constraints preventing from displaying the whole cell's view.
My code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"STI";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
}
cell.textLabel.text = self.dwarves[indexPath.row];
cell.detailedLabel.text = #"Detailed text";
UIImage *img = [UIImage imageNamed:#"star"];
cell.imageView.image = img;
return cell;
}
What I get:
I think the property you're looking for is cell.detailTextLabel, not cell.detailedLabel.
Take a look at this post.
The code I use for setting up my detailed text:
if (indexPath.row < 7) {
cell.detailTextLabel.text = #"First";
} else {
cell.detailTextLabel.text = #"Second";
}
I'm in the process of updating an old app for iOS 8 and ran into the same issue. I haven't discovered the root of the problem yet, but calling layoutSubviews for the cell before returning it fixed it for me.
...
[cell layoutSubviews];
return cell;
}
i am able to fetch images asynchronously on to uitableview.i am fetching these images are from a url.on scrolling up uitableview these images disappear and they take time to load again and sometimes they dont load at all.i dont want to use any 3rd party libraries.i dont want to go with synchronous approach.please suggest any correct approach to improve performance.thanks for help in advance.my code is below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=#"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"identifier"];
}
UIImageView *imgVw=[[UIImageView alloc]initWithFrame:CGRectMake(250, 10, 40, 30)];
[cell.contentView addSubview:imgVw];
Attributes *att = [listOfObjects objectAtIndex:indexPath.row];
strImgUrl=#"http:image url";
strImgName=att.classifiedImg;
if (strImgName == nil) {
UIImage *myImg=[UIImage imageNamed:#"user_circle.png"];
imgVw.image=myImg;
}
else{
strImg=[strImgUrl stringByAppendingString:strImgName];
}
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData *data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:strImg]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *img=[UIImage imageWithData: data];
imgVw.image=img;
});
});
return cell;
}
UITableViews are designed to reuse cells. When you scroll up for example the first cell might get reused to show the 5th cell since the 1st one is now off screen. When you scroll back up cellForRowAtIndexPath is called again, and you are async downloading the same image again. If you want to have it load instantly you will need to cache the images after downloading them the first time so the next time that image is needed you can directly pull it from the cache.
Many third party libraries do this (AFNetworking), but if you don't want to use them, you will have to cache the images manually.
It seems like your reuse code contains bug, which causes everytime to create new cells. The following code uses reuseIdentifier as #"identifier"
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"identifier"];
}
Change it to:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
EDIT:
On reusing tableView cells, you dont need to create and add subviews to cell each time. Instead, just create the subview when creating cell and then if cell is reusing, just get the subview using tag and update your content from datasource.
Your code can be modified like below:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
UIImageView *imgVw=[[UIImageView alloc]initWithFrame:CGRectMake(250, 10, 40, 30)];
imgVw.tag = indexPath.row;
[cell.contentView addSubview:imgVw];
}
UIImageView imgVw = (UIImageView*)[cell.contentView viewWithTag:indexPath.row];
//Rest is same as you posted.
Hope this will fix the issue.
I've a grouped UITableView and I've set the background of one of its cells to be a gradient image, this way:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
cell.textLabel.text = NSLocalizedString(#"Add item", #"");
cell.textLabel.backgroundColor = [UIColor clearColor];
UIImage* image1 =[[UIImage imageNamed:#"gradient_normal"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
UIImage* image2 =[[UIImage imageNamed:#"gradient_selected"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
cell.backgroundView = [[UIImageView alloc] initWithImage:image1];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:image2];
}
return cell;
}
Background of the cell is correctly displayed, but it has not rounded corners anymore, as the rest of the cells at top and bottom of the table and with default background have... I tried setting the cornerRadius property of the table view, but it didn't work. And I was not able to set the cornerRadius for the particular cell.
The only post I found dealing with this same problem had not been actually answered (grouped tableViewCell - no rounded corner after setting the background of a grouped table cell), could somebody help me with this issue?
Thanks in advance
If you are allowed/able to use outside libraries, checkout PrettyKit, makes it quite easy.
If you have to do it on your own, the best way to go about this is to subclass UITableViewCell and create your own custom cells. Here is a good tutorial
I want to show an image on the right side in a UITableViewCell.
I'm using this code, to do it:
CGRect imageRect = {80,0,225,44};
UIImageView *uiv = [[UIImageView alloc] initWithFrame:imageRect];
[uiv setImage:[UIImage imageNamed:[NSString stringWithFormat:#"star%#.png", [[self reataurant] valueForKey:#"stars"]]]];
[uiv setClipsToBounds:YES];
[cell addSubview:uiv];
My problem is, that the image is shown in other cells, when i'm scrolling the tableview.
What am i making wrong?
What is probably happening is that you are re-using cells that already have an image when you do not wish to. Check
a) when you re-use a cell, if it has the star image then remove it.
b) before you return the cell add the star at that point (with whatever test you use to determine if the star should appear).
Those UITableViewCells are reused. Look in your cellForRowAtIndexPath for
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
If you only want one or a certain set of cells to have this particular layout, you can pass a different cellIdentifier for these cells.
You can easily add an image view as the accessory view to a table cell for the same effect.
Here is the sample code for your understanding:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"fileName"]];
cell.accessoryView = imageView;
[imageView release];
And make sure you are reusing cell space of a table view in cellForRowAtIndexPath method by using a cell identifier so that overwritten problem can be eliminated ;)
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
Hope that should eliminate your problem of images getting displayed on one another,thanks :)
I do not understand why my cell.detailTextLabel.text is null.
static NSString *CellIdentifier = #"Cell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyId];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
/*[[NSBundle mainBundle] loadNibNamed:#"CellTest2" owner:self options:nil];
cell = mealPlanCell;*/
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
//self.mealPlanCell = nil;
}
cell.detailTextLabel.text = #"Test";
It will let me set the cell.textLabel.text to a value but not the detail one.
I figured out the reason if you leave
if(cell == nil){...}
around the cell = [[[UITableViewCell alloc]...
then the cells will not re render and thus will not update the style and or details so if you remove the if() then this will allow you to re render each cell in the table view if you are changing style back and forth. This is not a good thing to remove though if you think about it cause then it is going to low the tables "speed" which probably wont be noticeable but it is something to consider looking into as a possible issue. This is not what I did to solve the problem personally cause I choose to take a different approach but it is something that worked when I tried.
I had same problem: UITableViewCell initWithStyle:UITableViewCellStyleSubtitle is not working
For those that search and find this question... and have same problem as me, where you are using a prototype cell and the style is not defined correctly in the storyboard.
If your rowHeight is not tall enough, the cell.detailTextLabel.text won't be visible even if the cell.textLabel.text is visible. A rowHeight of 22 seems to be the minimum with the default font.