UIImage not shown as expected - ios

- (void)showImageAtTableView:(UITableView *)tableView forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView indexPathsForSelectedRows].count) {
if ([[tableView indexPathsForSelectedRows] indexOfObject:indexPath] != NSNotFound) {
//cell is expanded
CGFloat frameHeight = [[self.widthArray objectAtIndex:indexPath.row] floatValue];
CGFloat frameWidth = [[self.heightArray objectAtIndex:indexPath.row] floatValue];
//CGFloat yOrigin = [self configureHeightForRowAtIndexPath:indexPath];
CGRect imageFrame = CGRectMake(0,
12,
frameWidth,
frameHeight);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
imageView.image = [UIImage imageNamed:#"testImage.jpg"];
imageView.contentMode = UIViewContentModeCenter;
//configue which cell
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:imageView];
}
}
}
the code above is supposed to add a UIImageView to an existing UITableViewCell when tapped. this is the line that call that method (showImageAtTableView:forRowAtIndexPath) :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
[self updateTableView];
//get and show the image at selected cell
[self showImageAtTableView:tableView forRowAtIndexPath:indexPath];
}
but this is the result when the tableviewcell is tapped:
http://imgur.com/4NMJLf6,XweOgQu
what did I do wrong?

You're not changing the height of the UITableViewCell.
I think a better approach would be to include the UIImageView in the xib / storyboard design. Assuming you have some kind of dto object storing the data showed in the cells, you can keep on them a boolean indicating whether or not the image should be showed.
On your didSelectRowAtIndexPath you'll update that boolean, and do a [tableView reloadData].
In your cellForRowAtIndexPath you'll hide / show the UIImageView according the boolean.
In your heightForRowAtIndexPath you'll check again the boolean value in order to update the height accordingly.
Hope it helps.

Related

UITableView not loading data with TabBar Controller

I have tableview in one of my controller. It is not loading data into custom labels and image view with tags. I checked each and every thing, delegates are attached and I am reloading the data in viewWillAppear. I do not understand, where is the problem. I added label and images and assign them tag. Only default label of tableview is showed.
NSString *cellIdentifier;
NSMutableArray *historyArray;
#implementation CloudHistory
-(void)viewDidLoad
{
[super viewDidLoad];
historyArray = [[NSMutableArray alloc]initWithObjects:#"history1",#"history2",#"history3",#"history4",nil];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
dispatch_async(dispatch_get_main_queue(), ^{
//Reload data in services table.
[_historyTableView reloadData];
});
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//Number of section in services table.
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [historyArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
cellIdentifier = #"HistoryCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UILabel *name = (UILabel*)[cell viewWithTag:40];
name.text =[historyArray objectAtIndex:indexPath.row];
UIImageView *image = (UIImageView*)[cell viewWithTag:44];
image.image = [UIImage imageNamed:#"rtb_logo"];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//Change the background color to stoker Cloud color.
cell.selectedBackgroundView = [UIView new];
cell.selectedBackgroundView.backgroundColor = [ UIColor colorWithRed:116.0/255.0 green:174.0/255.0 blue:220.0/255.0 alpha:1.0];
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(BOOL)prefersStatusBarHidden
{
return YES;
}
#end
maybe you can export the name and image , I think some of them maybe is invalid or nil .
UILabel *name = (UILabel*)[cell viewWithTag:40];
NSLog(#"name = %#",name); // is it valid ?
UIImageView *image = (UIImageView*)[cell viewWithTag:44];
NSLog(#"image = %#", image); // is it valid ?
if some of them invalid or nil,you can do something like this:
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 80, 44)];
name.text = #"name";
[cell addSubview:name];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 5, 44, 44)];
imageView.image = image;
[cell addSubview:imageView];
hope it helps.
Did you create a custom UITableViewCell using a xib file?
If so, you could register it in viewdidload.
Something like this
[self.tableView registerNib:[UINib nibWithNibName:#"xibName" bundle:nil] forCellReuseIdentifier:CellIdentifier];
Then your tableview can find your custom view outlets.
Hope this helps
Why don't you use a custom UITableViewCell with your UILabel and UIImageView? So you can access the cell property, like:
cell.name.text = [historyArray objectAtIndex:indexPath.row];
cell.image = [UIImage imageNamed:#"rtb_logo"];
Anyway looks like your UILabel and UIImage is getting the values, but it's not setting them to your cell. To test you can log the value of the UILabel just to check. I don't know if it will work but have you tried this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
cellIdentifier = #"HistoryCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *name = (UILabel*)[cell viewWithTag:40];
name.text =[historyArray objectAtIndex:indexPath.row];
[cell viewWithTag:40] = name; // I don't know if this works
// Check it out if the name.text is getting any value
NSLog(#"UILabel %#",name.text);
UIImageView *image = (UIImageView*)[cell viewWithTag:44];
image.image = [UIImage imageNamed:#"rtb_logo"];
[cell viewWithTag:44] = image; // I don't know if this works
return cell;
}

Content not resizing inside UICollectionViewCell on first instance

My UICollectionView has a UIImageView with autolayout constraints. I require a fixed number of cells in a row so need to scale the image if cell requires.
Even if I do not resize the cells, but set UIimageView in storyboard larger than the cells expected dimension the first time cell is populated the image size is incorrect, however when each cell is reused afterwards they scale perfectly.
The scaling works once cells are reused, but please what am I missing?
example view is the second call to reloaddata on the 'balls' lower middle of view. The first call filled the first 4 cells. The second call corrects the now reused cells, but fails to scale the 4 new cells content.
Below is some code as requested. generalBreakCollection is in focus :
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (collectionView == self.p1HiBreakCollection) {
breakBallCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"P1Cell" forIndexPath:indexPath];
UIImageView *collectionImageView = (UIImageView *)[cell viewWithTag:106];
ballShot *selectedBall = [self.p1BreakBalls objectAtIndex:indexPath.row];
cell.ball = [self.p1BreakBalls objectAtIndex:indexPath.row];
collectionImageView.image = [UIImage imageNamed:selectedBall.imageNameLarge];
return cell;
} else if (collectionView == self.p2HiBreakCollection) {
breakBallCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"P2Cell" forIndexPath:indexPath];
UIImageView *collectionImageView = (UIImageView *)[cell viewWithTag:108];
ballShot *selectedBall = [self.p2BreakBalls objectAtIndex:indexPath.row];
cell.ball = [self.p2BreakBalls objectAtIndex:indexPath.row];
collectionImageView.image = [UIImage imageNamed:selectedBall.imageNameLarge];
return cell;
} else {
// generalBreakCollection
breakBallCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"GeneralCell" forIndexPath:indexPath];
ballShot *selectedBall = [self.breakShots objectAtIndex:indexPath.row];
cell.ball = [self.breakShots objectAtIndex:indexPath.row];
cell.ballStoreImage.image = [UIImage imageNamed:selectedBall.imageNameLarge];
cell.ballStoreImage.highlightedImage = [UIImage imageNamed:[NSString stringWithFormat:#"highlighted_%#",selectedBall.imageNameLarge]];
cell.ballStoreImage.contentMode = UIViewContentModeScaleAspectFit;
return cell;
}
}
The resize block contains:
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView == self.generalBreakCollection) {
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
float cellWidth = screenWidth / 10.0; //Replace the divisor with the column count requirement. Make sure to have it in float.
CGSize size = CGSizeMake(cellWidth, cellWidth);
return size;
}
else {
CGSize defaultSize = [(UICollectionViewFlowLayout*)collectionViewLayout itemSize];
return defaultSize ;
}
}
I have my own UICollectionViewCell class with a UIImageView property ballStoreImage. I do not do so much with it, but I have linked it to the UIImageView inside the cell in the storyboard:
#property (strong, nonatomic) IBOutlet UIImageView *ballStoreImage;
The autolayout was not getting recognised until I added the code line:
[[cell contentView] setFrame:[cell bounds]];
So full block reads:
} else {
breakBallCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"breakCell" forIndexPath:indexPath];
[[cell contentView] setFrame:[cell bounds]];
ballShot *selectedBall = [self.breakShots objectAtIndex:indexPath.row];
cell.ball = [self.breakShots objectAtIndex:indexPath.row];
cell.ballStoreImage.image = [UIImage imageNamed:selectedBall.imageNameLarge];
cell.ballStoreImage.highlightedImage = [UIImage imageNamed:[NSString stringWithFormat:#"highlighted_%#",selectedBall.imageNameLarge]];
return cell;
}

UITableviewCell Image size changing on Click of cell?

I am trying to display name and images in UITableViewCell like the normal way
cell.textLabel.text=#"some name";
cell.imageView.image=[array objectAtIndex:indexPath.row];
and when i load the ViewController , I can see all images are coming correctly but when I tried to click on any of UITableviewCell image size is changing to big(please check the example screenshot).
Please help me to solve this problem.
Loading time it looks like
When I click on the first cell ,the cell image becomes big like below image
Please check my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.backgroundColor=[UIColor clearColor];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.backgroundColor=[UIColor clearColor];
if([userName count]>0){
cell.textLabel.text=[userName objectAtIndex:indexPath.row];
[cell.imageView setImageWithURL:[NSURL URLWithString:[[userList valueForKey:#"photo"] objectAtIndex:indexPath.row]]
placeholderImage:[UIImage imageNamed:#"avatar.png"]];
}
CGSize itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.layer.masksToBounds=YES;
cell.imageView.layer.cornerRadius=9.0;
return cell;
}
My Didselect method is like this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *nib;
if ([[MyDevice getMyDevice] isiPad])
{
nib = #"nibname_iPad";
}
else
{
nib = #"nibname";
}
ViewController *chat = [[ViewController alloc] initWithNibName:xibName bundle:[NSBundle mainBundle]];
chat.hidesBottomBarWhenPushed=YES;
[self.navigationController pushViewController:chat animated:YES];
}
Try to use Custom Cells (Change the Style of the cell in the attributes inspector of the cell), its better and you can design it as the way you want. Create in the Story board the UILabels and the UIImage (with a specific width and height), finally in the attributes inspector of the UILabel and UIIamge set them with a specific Tag number, this number has to be unic for both of them. And access them like this:
In the method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Insert this:
UITableViewCell *customCell = [self.tableView dequeueReusableCellWithIdentifier:#"theNameOfTheCell"];
UILabel *label;
label = (UILabel *)[customCell viewWithTag:1];
label.text = #"Some text to label with tag number 1";
label = (UILabel *)[customCell viewWithTag:2];
label.text = #"Some text to label with tag number 2";
UIImageView *imageCell;
imageCell = (UIImageView *)[customCell viewWithTag:3];
CALayer * l = [imageCell layer];
UIImage *cellImage = [UIImage imageNamed:#"theImageYouWant.png"];
imageCell.image = cellImage;
Try with this and tell what append, this is the full Documentation: Official Documentation Table View Cells
Add content mode:
[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];
Hope this helps... :)

Custom Cells Overlapping in UITableView

I have a custom cell class called GameCell, the UI being created in storyboard. When my cells load, they load on top of each other. This problems occurs for cell.clipsToBounds YES and NO, just in different variations:
I have also tried [cell setClipsToBounds:(BOOL)] with no success.
Here's my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"gameCell";
GameCell *cell = (GameCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView.superview setClipsToBounds:NO];
NSLog(#"made a cell");
PFObject *myPartners = [self.games objectAtIndex:indexPath.row];
PFUser *partner = [self.partnerList objectAtIndex:indexPath.row];
// Cell profile image
cell.profileImage.layer.cornerRadius = cell.profileImage.frame.size.width / 2;
cell.profileImage.clipsToBounds = YES;
if([partner objectForKey:#"profilePic"]!=nil){
cell.profileImage.image = [partner objectForKey:#"profilePic"];
}
else {
cell.profileImage.image = [UIImage imageNamed:#"smile_green.png"];
}
//Cell indicators
if((int)[myPartners objectForKey:#"sent"]==1){
cell.myIndicator.image = [UIImage imageNamed:#"arrow_icon_dbl.png"];
}
else if ([myPartners objectForKey:#"sent"]==0){
cell.myIndicator.image = [UIImage imageNamed:#"arrow_icon.png"];
}
cell.partnerName.text = [myPartners objectForKey:#"receiverName"];
cell.gameId = myPartners.objectId;
// Cell drop shadow
[cell.cellView.layer setShadowColor:[UIColor blackColor].CGColor];
[cell.cellView.layer setShadowOpacity:0.5];
[cell.cellView.layer setShadowRadius:2.0];
[cell.cellView.layer setShadowOffset:CGSizeMake(1.0, 1.0)];
// Cell buttons
if([myPartners objectForKey:#"picture"]!=nil) {
[cell.myPlay setEnabled:NO];
} else {
[cell.myPlay setEnabled:YES];
}
return cell;
}
The view is a UITableViewController embedded in a NavigationController
You have used dequeueReusableCellWithIdentifier:
use like this
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell %d",indexPath.row];
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
and also check this link.
UITableView cell Contents are disappearing and overlapping when scrolled in UITableViewcell?
You should make sure that the height of your cell is the RowHeight of your UITableView control.
e.g.,
Cell is designed from XIB, and the height of the view is 30, then make sure:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30.0f;
}
In attributes inspector cancel Adjust Scroll view insets option.
you need clicked view controller .

Improper functioning tableView data and recharging

-Hi, I have a problem with a tableview reloading data and what I want to do is that you put the selected cell in blue and change an image that is, the problem is that if I make the [self.mytableview reloadData] in the didselectedRow blue background disappears and if I do the image of the cell does not change, I'm a bit lost with this piece of code I give thanks
if (indexPath.row == _selectedRow) {
UIImageView *favView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"favIconSelected.png"]];
CGRect frame = favView.frame;
frame.origin.x = 294;
frame.origin.y = 7;
favView.frame = frame;
[cell.contentView addSubview:favView];
[favView release];
}
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:SELCETED_BGIMGCELL]]autorelease];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Product *p = [_productList objectAtIndex:indexPath.row];
_selectedRow=indexPath.row;
[_delegate productWasSelected:p];
[self.myTableView reloadData];
}
You don't need to call reloadData at all. All you need to do is to update the cell that was selected.
In the didSelectRowAtIndexPath method you can get the cell directly by calling cellForRowAtIndexPath on the tableView. This will return the actual cell that's on display and you can directly set the image it's displaying.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = ...;
Alternatively you could control the image display from your cell subclass if you provide it with both the normal and selected images when you configure each instance.

Resources