Change the frame of a custom table view cell's button - ios

In my app I display a list of items for sale in a UITableView. If the user didn't set a description on an item, I would like to adjust the positioning of some of the elements in the cell to adjust for the blank label.
The following code in my cellForRowAtIndexPath isn't working.
cell.productName.text = product.fields[#"title"];
cell.productPrice.text = product.fields[#"price"];
cell.productDescription.text = vehicle.fields[#"salespersonComments"];
if ([cell.productDescription.text isEqualToString:#""]) {
CGRect f = cell.favoriteButton.frame;
f.origin.y = 10; // new y
cell.favoriteButton.frame = f;
} else {
}

//You have to use the UITableView delegate methods
//Method for displaying the cell
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
}
//Method for adjusting the cell height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}

Rather than physically changing the coordinated/frame of the item, it would be better to have IBOutlets connected to the relevant NSLayoutConstraints.
Then you could do something like:
if ([cell.productDescription.text isEqualToString:#""]) {
cell.favoriteButtonWidthConstraint.constant = 0;
cell.leadingIndentConstraint.constant = 0;
} else {
}

Related

How to hide a particular row button in Dynamic UITableViewCell in Objective C?

I have implemented a Dynamic TableView, Textfield and Buttons. My problem is, when I hide the button in the first row of my UITableViewCell, the other five rows of cell button also get hidden.
Can any one suggest a solution for this issue?
I have tried below code..
ladiesdetails=[[NSMutableArray alloc]initWithObjects:#"2",#"0",#"0",#"0",#"0",#"0", nil];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell1";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[passengerdetailcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([[ladiesdetails objectAtIndex:indexPath.row] intValue]==2)
{
cell.malebutton.hidden=yes;
}
return cell;
}
Just put the else condition and make the button visible in cellForRowAtIndexPath method. If you have any other condition to show add that as well.
if([[ladiesdetails objectAtIndex:indexPath.row] intValue] == 2) {
cell.malebutton.hidden = YES;
} else {
cell.malebutton.hidden = NO;
}
like this
bool flag = ([[ladiesdetails objectAtIndex:indexPath.row] intValue] == 2)
cell.malebutton.hidden = flag
It happens because of cell reuse , you use allocate the cell like below one .
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PassengerDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PassengerDetailCell" forIndexPath:indexPath];
if([[self.ladiesdetails objectAtIndex:indexPath.row] intValue]==2)
{
cell.maleButton.hidden = TRUE;
}
return cell;
}

iOS: UITableView first cell content with different type is not displayed

I have an UITableView in my UIViewController. The first cell has type1 and the others type2. My problem is that the content of my first cell is never displayed.
This is my code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0) {
return 240;
}
return 90;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == 0) {
Type1Cell * tmpCell1 = [tableView dequeueReusableCellWithIdentifier:#"type1Cell"];
if (tmpCell1 == nil) {
tmpCell1 = [[Type1Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"type1Cell"];
}
//set data
return tmpCell1;
} else {
Type2Cell * tmpCell = [tableView dequeueReusableCellWithIdentifier:#"type2Cell"];
if (tmpCell == nil) {
tmpCell = [[Type2Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"type2Cell"];
}
//set data
return tmpCell;
}
}
The first cell has the same specified height, but it's always blank. I don't have this problem with the other cells.
Make sure Cell content View or any-other mistakenly checked Hidden property to TRUE.
You need to register your cell as well.
UINib *cellNib = [UINib nibWithNibName:#"Type1Cell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:#"type1Cell"];

UITableViewCell with UIScrollView cause content of other cells disappearing

I was trying to find solution almost everywhere, but I didn't find it. So, here is my problem.
I have UITableView with custom UITableViewCells.
The first cell has UIScrollView inside its Content View.
The Second cell has UILables and other basic views inside its Content View.
So, if there is UIScrollView inside the first cell, content of the second cell disappears. It appears only if the first cell scrolls out of the tableView frame.
Can anybody help me figure it out? Thank you.
Code preview
#pragma mark - UITableView Data Source
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqual:_photosIndexPath]) {
static NSString *PhotosCellIdentifier = #"AdDetailsPhotosCell";
BazarAdDetailPhotosCell *cell = [tableView dequeueReusableCellWithIdentifier:PhotosCellIdentifier];
if (!cell) {
cell = [[BazarAdDetailPhotosCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PhotosCellIdentifier];
}
cell.photoScrollView.scrollsToTop = NO;
cell.photoScrollView.delegate = cell;
[cell setPhotos:_adDetail.photos];
return cell;
}
else if ([indexPath isEqual:_adDetailsPath]) {
static NSString *DetailsCellIdentifier = #"AdDetailsDetailCell";
BazarAdDetailsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailsCellIdentifier];
if (!cell) {
cell = [[BazarAdDetailsDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DetailsCellIdentifier];
}
cell.adTitleLabel.text = _adDetail.title;
cell.priceLabel.text = _adDetail.price;
// this cell content disappears
}
}
Might be issue with cell drawing on iOS 7.1, according answer on iOS 7.1 beta5 tableviewcell height showing objects outside it's range, try to clip subviews:
cell.clipsToBounds = YES;
Try it
#pragma mark - UITableView Data Source
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath isEqual:_photosIndexPath])
{
static NSString *PhotosCellIdentifier = #"AdDetailsPhotosCell";
BazarAdDetailPhotosCell *cell = [tableView dequeueReusableCellWithIdentifier:PhotosCellIdentifier];
if (!cell) {
cell = [[BazarAdDetailPhotosCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PhotosCellIdentifier];
}
cell.photoScrollView.scrollsToTop = NO;
cell.photoScrollView.delegate = cell;
[cell setPhotos:_adDetail.photos];
return cell;
}
else {
static NSString *DetailsCellIdentifier = #"AdDetailsDetailCell";
BazarAdDetailsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailsCellIdentifier];
if (!cell) {
cell = [[BazarAdDetailsDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DetailsCellIdentifier];
}
cell.adTitleLabel.text = _adDetail.title;
cell.priceLabel.text = _adDetail.price;
// this cell content disappears
return cell;
}
}

UITableViewController duplicating rows?

I have a UITableViewController that is duplicating rows as I scroll down. I understand that the cells are being reused, but I just can't seem to figure out what to do to fix it. I've read through other similar posts and couldn't figure out a solution in my case.
What am I doing wrong in my code that's resulting in duplicated cells?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"StringTableViewCell";
if (indexPath.row == 0) {
cellIdentifier = #"StringTableViewCell";
} else if (indexPath.row == 1) {
cellIdentifier = #"StringTableViewFooter";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// Sets up the table view cell accordingly based around whether it is the
// string row or the footer
if (cell) {
if ([cell isKindOfClass:[StringTableViewCell class]]) {
StringTableViewCell *stringCell = (StringTableViewCell *)cell;
// Sets the photos to the array of photo data for the collection view
[stringCell setCollectionData:self.images];
} else if ([cell isKindOfClass:[StringFooterTableViewCell class]]) {
// Sets the table view cell to be the custom footer view
StringFooterTableViewCell *footerCell = [[StringFooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"StringTableViewCellFooter"];
// Init's the footer with live data from here
[footerCell setStringUploaderName:#"Name"];
[footerCell setStringUploadDate:#"10 minutes ago"];
[footerCell setStringUploaderProfileImage:[UIImage imageNamed:#"avatar.jpg"]];
[footerCell setCommentButtonTitle:#"4.7k"];
[footerCell setLikeButtonTitle:#"11.3k"];
return footerCell;
}
}
return cell;
}
There's a prepareForReuse method for UITableViewCell which is called every time it's about to be reused. You can set your StringTableViewCell's imageview's image to nil there or you can just do it on the cell for indexPath itself, as the prepareForReuse method is only encouraged to be used in resetting the state(like hightlighted, or selected) for the cell and it's subviews.
Since you have two different cells you need something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
static NSString *cellIdentifier = #"StringTableViewCell";
StringTableViewCell *stringCell = (StringTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!stringCell) {
stringCell = [[StringTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[stringCell setCollectionData:self.images];
return stringCell;
} else if (indexPath.row == 1) {
static NSString *cellIdentifier = #"StringTableViewFooter";
StringFooterTableViewCell *footerCell = (StringFooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!footerCell) {
footerCell = [[StringFooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// Init's the footer with live data from here
[footerCell setStringUploaderName:#"Name"];
[footerCell setStringUploadDate:#"10 minutes ago"];
[footerCell setStringUploaderProfileImage:[UIImage imageNamed:#"avatar.jpg"]];
[footerCell setCommentButtonTitle:#"4.7k"];
[footerCell setLikeButtonTitle:#"11.3k"];
return footerCell;
} else {
return nil; // this shouldn't happen but makes the compiler happy
}
}
Of course the code needs to be updated to show the data appropriate to the given section.

UITableViewCell with UIImageView in a few of them

I have a customized UITableViewCell and I've added a UIImageView to it. The thing is, there are times when I want to remove the UIImageView as the data I receive from the server does not have an image associated with it. How do I make the cell layout change such that the other elements move up. Should I set the UIImageView's frame to zero? Using a placeholder image is not a solution.
Here's what my problem looks like:
The row with index 0 does not have an image but there is a space in between the top and the bottom label. Is there a way I can do this using Auto Layout?
Your problem is actually caused by heightForRowAtIndexPath, you have to return the proper value in this method, something like:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
YourCustomObject *object = [tableDataSource objectAtIndex:indexPath.row];
if(object.hasImage) {
return heightForCellWithImageInside;
}
else {
return heightForCellWithoutImageInside;
}
}
Then, in your cellForRowAtIndexPath just hide the UIImageView if there is no image for it.
You can dynamically change the height of cell.If there is an image,then increase cell height otherwise not.Also set the imageView whenever u get image from server otherwise no need to set imageView.like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ImageCellItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(imageFromserver)
{
//Set your imageView here
}
else
{
// Do nothing
}
return cell;
}
when ever there is no image coming from server hide or remove your image view and assign the frame of image view to your bottom label.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
}
if(yourimage==nil)
{
bottomlabel.frame=imageview.frame; // you can do something like this, as per your need.this is just example
}
return cell;

Resources