I have made a custom cell and i want to change the frame of contentView. My cell is a CustomCell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"hey");
static NSString *CellIdentifier = #"CustomCellReuseID";
AbcCustomCell *cell = [tabel_Abc dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[AbcCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *name= [arrayTabel_Abc objectAtIndex:indexPath.section];
[cell setBackgroundColor:[UIColor clearColor]];
[cell.label_CellList setText:name];
cell.contentView.frame = CGRectMake (100,0,500,20);
return cell;
}
I have use
cell.contentView.frame = CGRectMake (100,0,500,20);
but its not working for me. I want to change the contentView of customCell, Can any one guide me?? Thanks in advance.
You can't change the contentView. UITableViewCell contentView's height is determined by – tableView:heightForRowAtIndexPath: delegate method and its width is the tableView's width normally.
If you can add a view as subview of contentView and change the view's frame.
You need to re-assign the frame for the cell's contentView.
CGRect oldFrame = cell.contentView.frame;
cell.contentView.frame = CGRectMake( oldFrame.origin.x,
oldFrame.origin.y,
oldFrame.size.width + 20,
oldFrame.size.height );
or you may use custom cell
you can't change the frame of contentView of a cell. you can change the height of a cell by using
tableView:heightForRowAtIndexPath:
But you can't change the width.
Also you can use a view if you want to make as if the divider is big, if you want something like that.
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width - paddingX*2, 1)];
separatorLineView.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:separatorLineView];
Can you be more specific to which frame you want to change(whether it is out of bounds)?
Related
Hi i am a newbie to iOS.
I have implemented a tableview in ViewController1.In the tableview i am displaying tittle,detailText and disclosure indicator for 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:UITableViewCellStyleValue1
reuseIdentifier:MyIdentifier];
}
cell.textLabel.text =[listTableArray objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor grayColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.text=#"4mile";
return cell;
}
Everything works fine,Now i need a image before the disclosure indicator when i do it with
UIImageView *accessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
accessoryView.image=[UIImage imageNamed:#"list.png"];
cell.accessoryView = accessoryView;
Disclosure indicator is replaced by the image.But i need both image along with disclosure indicator without using Custom cell.
How can i do it...? Help me out.
Thanks
similiar to the due date but at the place of time i need image
UITableViewCell have a fixed layout, depending on the style you use when you initialize it. You cannot change that layout, since the cell lays out its subviews as it prefers to have them.
add the - (void)layoutSubviews to your cell subclass like this:
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0.0f , 0.0f, 20.0f, 20.0f);
}
else try this
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:#"list.png"];;
[imageView setFrame:CGRectMake(50, 5, 20, 20)]; // customize the frame
[cell.contentView addSubview:imageView];
If you wanna add UIImageView to standard UITableViewCell you need add your imageView to contentView of cell and add all subviews like a textLabel, detailTextLabel and accessoryView
UIImageView *accessoryImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
accessoryImageView.image=[UIImage imageNamed:#"list.png"];
[cell.contentView addSubview:accessoryImageView];
or subclass of UITableViewCell and implement all views inside it
I have a static cell that stores an information form which user can input fields. One of the static cell is for attachments. I would like to append some UIView into this cell when the user chose an image from the UIImagePickerController. I have already handled the UIImagePickerController part. And I have find the cell by reuseIdentifier:
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"AttachmentCell"];
And I tried to append views in my cell:
CGRect attachmentFrame = CGRectMake(0, 0, 50, 50);
UIView * attachmentRow = [[UIView alloc]initWithFrame: attachmentFrame];
CGFloat ratio = 50 / image.size.width;
CGRect imageFrame = CGRectMake(10, 0, image.size.width * ratio, 50);
CGRect imageNameLabelFrame = CGRectMake(10 + imageFrame.size.width + 10, 22, 300, 6);
UIImageView *imageView = [[UIImageView alloc]initWithFrame: imageFrame];
imageView.image = image;
UILabel *imageNameLabel = [[UILabel alloc]initWithFrame:imageNameLabelFrame];
imageNameLabel.text = [NSString stringWithFormat:#"Attachment %d", attachmentCounter++];
[attachmentRow addSubview: imageView];
[attachmentRow addSubview: imageNameLabel];
attachmentRow.backgroundColor = [UIColor lightGrayColor];
[cell.contentView addSubview: attachmentRow];
NSLog(#"Row appended");
Notice that the above code is just to try adding one single view in the static cell but it seems failed. Nothing shows in my static cell after I add the view. I have logged that my attachment data can successfully brings to this page.
Maybe this is just the wording in the question, but it seems that what you do after the user has selected the image, is that you create a new cell.
This line :
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"AttachmentCell"];
Creates a brand new cell, and your table view has no way to know about it.
What you should rather do is to reload your table view after the user has selected the image and setup it in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
In order to reload the data you can use either reloadData method of UITableView which will reload all data or - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *) withRowAnimation:(UITableViewRowAnimation)animation which will reload only cells at specifed indexes.
firstly add this line
[cell.contentView addSubview: attachmentRow];
then
[attachmentRow addSubview: imageView];
[attachmentRow addSubview: imageNameLabel];
may be it will help
You will have to use custom cell instead of using default tableview cell.
For that first you need to create one with identifier.
I have a UITableView with different cell heights. I have added a custom separator line also using the following code
UIView* separatorLineView = [[UIView alloc]initWithFrame:CGRectMake(1, cellReport.frame.size.height-1, table.frame.size.width-1, 1)];
separatorLineView.backgroundColor = [UIColor grayColor];
[cellReport.contentView addSubview:separatorLineView];
When i scroll my table extra separators appear between the rows and i dont understand why? Am i missing something? help me out.
In short UITableView displays separator at wrong position for some cells. Have attached an example image for reference.
P.S: I AM NOT USING AUTOLAYOUT
Please follow these steps
And do not need to every time your separator view. And set your frame every time.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *myIdentifier=#"Cell";
UITableViewCell *Cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:myIdentifier];
UIView* separatorLineView;
if (Cell == nil)
{
Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
separatorLineView = [[UIView alloc] initWithFrame:CGRectZero];
separatorLineView.backgroundColor = [UIColor grayColor];
separatorLineView.tag = 100;
[Cell.contentView addSubview:separatorLineView];
}
separatorLineView = (UIView *)[Cell.contentView viewWithTag:100];
separatorLineView.frame = CGRectMake(0, Cell.frame.size.height-1, Cell.frame.size.width, 1);
}
STEP 1 - this is working fine
I have UITableView that loads custom UITableViewCells
STEP 2 - this is kinda works
I change the UITableViewCell height so that all the data contained in the cell within a UITextView is visible
I manage to get the UItextView data and resize it by using the following code:
UITextView *dummy = [[UITextView alloc] init];
dummy.font = [UIFont systemFontOfSize:14];
dummy.text = cell.textView.text;
CGSize newSize = [dummy sizeThatFits:CGSizeMake(270.0f, 500.0f)];
//get the textView frame and change it's size
CGRect newFrame = cell.textView.frame;
newFrame.size = CGSizeMake(270.0f, fmaxf(newSize.height, 60));
cell.textView.frame = newFrame;
//resize the cell view I add +95 because my cell has borders and other stuff...
CGRect cellFrame = CGRectMake(0, 0, 320, newFrame.size.height+95);
cell.cellView.frame = cellFrame;
I then manage to set the UITableViewCell height using the delegate function heightForRowAtIndexPath:
Now when I run the code and scroll up and down the table cells the behavious isn't always as expected... i.e the cell size isn't always the right size, but if I scroll up and down it sometimes loads up the right size again.
I am thinking that perhaps the dequeueReusableCellWithIdentifier is the issue
cellIdentifier = #"tableCell";
ctTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
Since I am recycling cells of different size with the same identifier...
Can anybody give me some guidelines on how best to solve this issue?
Thanks!!!
I would start by adding UITextView dynamically inside cellForRowAtIndexPath method. assumptions(data array contains the content to be displayed inside cell)
// Defines
#define CELL_TEXTVIEW_WIDTH = 320
#define CELL_TEXTVIEW_HEIGHT = 9999
#define PADDING = 5
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"tableCell";
ctTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if(cell == nil){
cell = [[tableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *_data = [data objectAtIndex:indexPath.row];
CGSize _data_contentsize = [_data sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(CELL_TEXTVIEW_WIDTH, CELL_TEXTVIEW_HEIGHT) lineBreakMode:NSLineBreakModeWordWrap];
UITextView *dummy=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, _data_contentsize +(PADDING * 2))];
// add Font and text color for your textview here
[cell.contentView addSubview:dummy];
return cell;
}
After this calculate the height of the cell under heightForRowAtIndexPath method.
have you implemented following method ?
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
you should set height for each row , if they are different from each other
i need to have 2 kinds of rows. One with 44 px of height and other with 90px:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0)
return 90.0;
else
return 44.0;
}
The problem is when i called - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath because i need to add to the cell with 90px an scrollView that have 90px of height but it's added on the middle of the cell instead on the top.
if (indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:ScrolledCellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ScrolledCellIdentifier];
//cell.textLabel.text = #"hi";
[cell addSubview:self.scrollView];
}
Trying to set the textLabel of the cell works, the problem is adding the scrollView.
If i add this line to the code:
self.scrollView.frame = CGRectMake(0,-45, 320, 90);
It works perfectly but it sounds some weird for me. What is happening?
Set the frame of your scrollView to the bounds of your cell before you add it.
So
self.scrollView.frame = cell.bounds;
[cell addSubview:self.scrollView];
Debug... NSLog.... output the frames of things so you can see where they are getting positioned as opposed to where you want them to be positioned. Use this for outputting frame coordinates:
NSLog(#"Frame is: ", NSStringFromCGRect(self.scrollView.frame));
or define a nice shortcut, as I use this all the time
#define LogCGRect(rect) NSLog(#"CGRect:%#", NSStringFromCGRect(rect));
then you can just do
LogCGRect(self.scrollview.frame);
It just fits the scrollview into the tableviewcell, no matter if it's height is 44 or 90.
I solved my problem using this code:
cell = [tableView dequeueReusableCellWithIdentifier:ScrolledCellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ScrolledCellIdentifier];
cell.frame = CGRectMake(0, 0, 320, 90);
self.scrollView.frame = CGRectMake(0,0, 320, 90);
[cell addSubview:self.scrollView];
i don't know if it's wrong but it works...