This is really stumping me. I know how to change the height of a UITableViewCell in heightForRowAtIndexPath. The issue if that I am trying to get two cells to sometimes overlap - so sometimes I need the frame of the cell in question to be higher than the height of the row. I know how to do this for the initial display of the cell by subclassing the custom cell and overriding the setFrame method. But once the cell displays, it seems that I cannot manually change the frame. Again - I am not talking about using heightForRowAtIndexPath here or tableview.rowHeight - I am talking about leaving the row height and manually changing the frame of the cell (possibly multiple times) after it has been displayed. Can anyone help me out?
Ok i dont no i got ur question or not, comment if there is any thing missed, i am posting the sample code to change the frame of custom cell manually by setting the different frame for each cell, in my code by tapping a button the frame of last cell is changed each time. Try it out this might u needed i dont no what u exactly want. Hope this Helps :)
//in CustomCell.h file
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
{
}
#property(nonatomic, assign) CGRect frameRect;
#end
//CustomCell.m file
#import "CustomCell.h"
#implementation CustomCell
#synthesize frameRect;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews
{
// always try to set frame in layoutSubviews
[super layoutSubviews];
self.frame = frameRect;
}
//in ViewController.h file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<CountdownTimerDelegate>
- (IBAction)changeFrame:(id)sender;
#end
//ViewController.m file
#import "ViewController.h"
#import "CustomCell.h"
#interface ViewController ()<UITableViewDataSource, UITableViewDelegate>//delegate
{
CGRect aChangableRect; //your changeable frame
int k;
int yPos;
}
- (void)viewDidLoad
{
k = 25;
yPos = 220;
aChangableRect = CGRectMake(10,440, 50, 30);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//initially set it no for all cell
CustomCell *cell = [self.aTableView dequeueReusableCellWithIdentifier:#"cell"];
if(cell == nil)
{
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
cell.frameRect = CGRectMake(13, 5, 150, 50);
}
else if (indexPath.row == 1)
{
cell.frameRect = CGRectMake(5,60 , 200, 45);
}
}
else if(indexPath.section == 1) //second section
{
if(indexPath.row == 0)
{
cell.frameRect = CGRectMake(13, 110, 150, 50);
}
else if (indexPath.row == 1)
{
cell.frameRect = CGRectMake(5,165 , 200, 45);
}
}
else
{
// cell.frameRect = aChangableRect; //for last cell of last section
if(indexPath.row == 0)
{
cell.frameRect = CGRectMake(13, 220, 150, 50);
}
else if (indexPath.row == 1)
{
cell.frameRect = aChangableRect;
}
}
cell.textLabel.text = #"happy coding";
return cell;
}
//i am cganging the frame of last cell each time button clicked
- (IBAction)changeFrame:(id)sender
{
aChangableRect = CGRectMake(25, 450 , k * 2, k * 3);
[self.aTableView reloadData];
k = k + 10;
}
Related
I'm trying to have dynamic cell heights. I've created a custom cell programmatically. Now I want the cell to change in height, but it seems to always be returning 44 which is the default tableviewcell height. Why is this?
- (void)viewDidLoad {
[super viewDidLoad];
[self.reminderTableView registerClass:[ReminderTableViewCell class] forCellReuseIdentifier:#"Daybreak-Cell"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
{
if (indexPath.row > 3) {
return 60;
}
return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"Custom-Cell" forIndexPath:indexPath];
...
return cell;
}
Custom Cell
#implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSLog(#"HEIGHT: %f",self.frame.size.height);
// Initialization code
[self.contentView setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, kDefaultTableViewCellHeight)];
[self setBackgroundColor:[UIColor clearColor]];
[self createViews];
}
return self;
}
All you need to do, starting with iOS7 i believe, is:
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
to get the cell to be of a dynamic height, also don't forget to put constraints within your cell from top to the bottom
JUST ensure that you are setting your table's delegate to be the class containing
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
{
if (indexPath.row > 3) {
return 60;
}
return 70;
}
As this method works with me
I need to set the position of my map view in a table view cell. I simply set the frame in cellForRow method.
some of the map views are misplaced. When I scroll up the table and scroll down to let it reappear (to reuse the table cell), then it is fixed.
Note
the y position is wrong but the x position is correct.
I use the same way (simply set frame) to set the pictures position and they are always correct. So the problem is map view itself instead of how to position the frame.
The following screenshot shows 3 map views, the middle one has a wrong y position
EDIT:
The UI part is quite complicated. inside cellForRowAtIndexPath method, I dequeue a MessageCell and call its setupWithMessage:(Message *)message method and then I check if the message is of type location: (a location type message has an optional text view and a map view)
self.textView.hidden = NO;
self.mapView.hidden = NO;
self.textView.text = message.text;
[Helper setupMapView:self.mapView posx:message.posx posy:message.posy];
CGSize size1 = [UIHelper sizeWithText:message.text entity:message];
CGSize size2 = [UIHelper sizeWithMapEntity:message];
CGRect frame = [UIHelper adjustTextView:self.textView textViewSize:size1 extraView:self.mapView extraViewSize:size2 entity:message];
+ (CGRect)adjustTextView:(UITextView *)textView textViewSize:(CGSize)textViewSize extraView:(UIView *)extraView extraViewSize:(CGSize)extraViewSize entity:(id)entity {
if (textView == nil || extraView == nil || CGSizeEqualToSize(textViewSize, CGSizeZero) || CGSizeEqualToSize(extraViewSize, CGSizeZero)) {
CGSize targetSize;
UIView *targetView;
if (textView == nil || CGSizeEqualToSize(textViewSize, CGSizeZero)) {
targetView = extraView;
targetSize = extraViewSize;
}
else {
targetView = textView;
targetSize = textViewSize;
}
return [self adjustContentView:targetView size:targetSize entity:entity];
}
else {
CGRect frame1 = [self adjustContentView:textView size:textViewSize entity:entity];
CGRect frame2 = [self adjustContentView:extraView size:extraViewSize entity:entity];
frame2.origin.y += frame1.size.height + OFFSET_BETWEEN_TEXTVIEW_EXTRAVIEW;
extraView.frame = frame2;
return CGRectMake(frame1.origin.x, frame1.origin.y, MAX(frame1.size.width, frame2.size.width), frame1.size.height + frame2.size.height + OFFSET_BETWEEN_TEXTVIEW_EXTRAVIEW);
}
Note that the "extraView" above can refer to any view below a text view, such as an UIImageView
+ (CGRect)adjustContentView:(UIView *)contentView size:(CGSize) size entity:(id)entity {
float photoSideOffset = 60;
float topOffset = 30;
CGRect frame;
frame.origin.y = topOffset;
frame.size = size;
if ([DBHelper isMyEntity:entity]) {
frame.origin.x = 320 - size.width - photoSideOffset;
}
else {
frame.origin.x = photoSideOffset;
}
contentView.frame = frame;
if ([contentView isKindOfClass:[UITextView class]]) {
[(UITextView *)contentView sizeToFit];
}
return contentView.frame;
}
EDIT 2: i use the same UIHelper methods for photo messages and the coordinates are correct:
EDIT 3: the value of size1 size2 and frame
Approach 1: alloc init UITableViewCell inside your cellForRowAtIndexPath
Simple solution is to cell = [[UITableViewCell alloc]init]; alloc init every time
(it will make new cell each time instead caching/or reusing )
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
/*
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
*/
// Try below code
cell = [[UITableViewCell alloc]init];
------
-------
return cell;
}
Approach 2: removeFromSuperView
for (UIView *subview in self.view.subviews) {
if ([subview isKindOfClass:[UIButton class]]) { // set kind as per your own requirement
[subview removeFromSuperview];
}
}
Sample of code
MultiMap.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface MultiMap : UIViewController
<UITableViewDelegate,UITableViewDataSource,MKMapViewDelegate>
{
MKMapView *mapView;
}
#property (retain, nonatomic) IBOutlet UITableView *mainTableView;
#end
MultiMap.m
#import "MultiMap.h"
#interface MultiMap ()
#end
#implementation MultiMap
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
self.title=#"Multi Map";
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"multi";
UITableViewCell *cell=nil;
cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
mapView=[[MKMapView alloc]initWithFrame:CGRectMake(25, 25, 275, 150)];
mapView.delegate=self;
[mapView setBackgroundColor:[UIColor whiteColor]];
[mapView setTintColor:[UIColor whiteColor]];
[cell addSubview:mapView];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#""];
annotationView.canShowCallout = YES;
return annotationView;
}
- (void)dealloc {
[_mainTableView release];
[super dealloc];
}
#end
May you get help..
Please make sure your textView frame is 0. for the case the mapView doesn't show at accurate place additionally you can try following.
+ (CGRect)adjustTextView:(UITextView *)textView textViewSize:(CGSize)textViewSize extraView:(UIView *)extraView extraViewSize:(CGSize)extraViewSize entity:(id)entity {
if (textView == nil || extraView == nil || CGSizeEqualToSize(textViewSize, CGSizeZero) || CGSizeEqualToSize(extraViewSize, CGSizeZero)) {
CGSize targetSize;
UIView *targetView;
if (textView == nil || CGSizeEqualToSize(textViewSize, CGSizeZero)) {
targetView = extraView;
targetSize = extraViewSize;
}
else {
targetView = textView;
targetSize = textViewSize;
}
return [self adjustContentView:targetView size:targetSize entity:entity];
}
else {
CGRect frame1 = [self adjustContentView:textView size:textViewSize entity:entity];
CGRect frame2 = [self adjustContentView:extraView size:extraViewSize entity:entity];
frame2.origin.y += frame1.size.height + OFFSET_BETWEEN_TEXTVIEW_EXTRAVIEW;
extraView.frame = frame2;
[[extraView superview] setNeedsLayout]; //Addition
return CGRectMake(frame1.origin.x, frame1.origin.y, MAX(frame1.size.width, frame2.size.width), frame1.size.height + frame2.size.height + OFFSET_BETWEEN_TEXTVIEW_EXTRAVIEW);
}
UPDATE: Or you can update the following
+ (CGRect)adjustContentView:(UIView *)contentView size:(CGSize) size entity:(id)entity {
float photoSideOffset = 60;
float topOffset = 30;
CGRect frame;
frame.size = size;
if ([DBHelper isMyEntity:entity]) {
frame.origin.x = 320 - size.width - photoSideOffset;
}
else
{
frame.origin.x = photoSideOffset;
}
for (id view in [[contentView superview] subviews])
{
if (view != contentView)
{
if ([view respondsToSelector:#selector(frame)])
{
topOffset = (([view frame].origin.y + [view frame].size.height)<topOffset)?([view frame].origin.y + [view frame].size.height):topOffset;
}
}
}
if ((int)topOffset != 30)
{
topOffset += OFFSET_BETWEEN_TEXTVIEW_EXTRAVIEW;
}
frame.origin.y = topOffset;
contentView.frame = frame;
if ([contentView isKindOfClass:[UITextView class]]) {
[(UITextView *)contentView sizeToFit];
}
return contentView.frame;
}
Also make sure you update the height of the cell accordingly using - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
I am using grouped UITableview with custom cell.My tableview consists of two sections.I need to change textfield frame only for two rows in section zero.How to possible??Please help me.Go through my code
customcell.m
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.textfiled1 = [[UITextField alloc]init];
self.textfiled1.returnKeyType = UIReturnKeyNext;
textfiled1.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.contentView addSubview:self.textfiled1];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
-(void)layoutSubviews{
[super layoutSubviews];
self.textfiled1.frame = CGRectMake(50, 3, 250,40);
}
#pragma Tableview Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
static NSString *CellIdentifier = #"Cell";
customcell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[customcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSeparatorStyleNone;
}
cell.textfiled1.delegate = self;
if (indexPath.row==0) {
cell.textfiled1.frame = CGRectMake(50,3,180,40);//Change textfield frame
cell.separatorInset = UIEdgeInsetsMake(0, 50, 0, 100);
}
else if(indexPath.row==1)
{
cell.textfiled1.frame = CGRectMake(50,3,180,40);//Change textfield frame
}
else if(indexPath.row==2)
{
cell.textfiled1.keyboardType = UIKeyboardTypePhonePad;
}
else if(indexPath.row==3)
{
}
return cell;
}
else if(indexPath.section == 1)
{
static NSString *CellIdentifier1 = #"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
}
cell.textLabel.text = #“Section1";
return cell;
}
}
Insted of this make a property so that u can set the specified frame u want hear the code, in controller u need to set frame for each cell see the code below
//CustomCell.h file
#interface CustomCell : UITableViewCell
#property(nonatomic,assign) CGRect TextFieldFrame;//put this to change the frame
#property (nonatomic,retain)UITextField *textfiled1;
#end
//in CustomCell.m file
#import "CustomCell.h"
#implementation CustomCell
#synthesize TextFieldFrame;//sysnthesise
#synthesize textfiled1;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.textfiled1 = [[UITextField alloc]init];
self.textfiled1.backgroundColor = [UIColor greenColor];
self.textfiled1.returnKeyType = UIReturnKeyNext;
textfiled1.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.contentView addSubview:self.textfiled1];
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
self.textfiled1.frame = CGRectMake(self.bounds.origin.x + self.TextFieldFrame.origin.x, self.bounds.origin.y + self.TextFieldFrame.origin.y, self.TextFieldFrame.size.width, self.TextFieldFrame.size.height);
// self.textfiled1.frame = CGRectMake(50, 3, 250,40);//hear u are setting the contant frame thats wy frames are not changed
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
//in ViewController.m file
#import "ViewController.h"
#import "CustomCell.h"
#interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
#end
#implementation ViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
static NSString *CellIdentifier = #"Cell";
CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSeparatorStyleNone;
}
cell.textfiled1.delegate = self;
if (indexPath.row==0) {
cell.TextFieldFrame = CGRectMake(50,3,180,40);//Change textfield frame, u can set the frame for each cell hear
cell.separatorInset = UIEdgeInsetsMake(0, 50, 0, 100);
}
else if(indexPath.row==1)
{
cell.TextFieldFrame = CGRectMake(50,3,180,40);//Change textfield frame, heare also
}
return cell;
}
else if (indexPath.section == 1)
{
static NSString *CellIdentifier = #"Cell_2";
CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSeparatorStyleNone;
}
cell.textfiled1.delegate = self;
if(indexPath.row==0)
{
cell.textfiled1.keyboardType = UIKeyboardTypePhonePad;
cell.TextFieldFrame = CGRectMake(80, 3, 180, 40); //for other cells default frame u need to set it heare
}
else if(indexPath.row==1)
{
cell.textfiled1.keyboardType = UIKeyboardTypePhonePad;
cell.TextFieldFrame = CGRectMake(80, 3, 180, 40);
}
return cell;
}
else
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
Create a custom table view cell initializer and pass indexpath . So now in tableview cell you know the row, based on row number make the changes
I am trying to create a (not very) custom UITableView with a button on the left side of the cell, and text to the right of the button.
I try to calculate where to put the UILabel that is part of the cell but changing the frame has no effect (and it doesn't even appear to be computed -- all zeros).
My question is: when is the frame size for the label computed? (So that changes will have an effect).
Is this the right way to do it?
Code snippets below.
First is from the cell init (called in response to the dequeueReusable...
and yes, 'buttonColor', 'onTapSel', 'buttonFrame', and 'buttonColor' are "member variables (File static globals)
- (id)initWithStyle: (UITableViewCellStyle)style
reuseIdentifier: (NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (nil == buttonColor)
{ buttonColor = kDefaultCellButtonColor; }
if (nil == onTapSel)
{ onTapSel = #selector(defaultCellButtonTapped:); }
if (self)
{
EGCellButton * cellButton = (EGCellButton *)[[EGCellButton alloc ] initWithFrame:buttonFrame ];
cellButton.backgroundColor = buttonColor;
[cellButton setTitle:buttonLabel forState:UIControlStateNormal];
[cellButton addTarget:self action:(onTapSel) forControlEvents: UIControlEventTouchUpInside];
[self setCellButton:cellButton];
float xx = buttonFrame.origin.x + buttonFrame.size.width + 5;
CGRect frame = self.textLabel.frame;
frame.origin.x += xx;
self.textLabel.frame = frame;
[self addSubview:cellButton];
}
return self;
}
and now from the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"EGButtonTableCellID";
EGButtonTableCell * cell = (EGButtonTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.cellButton.indexPath = indexPath;
// Configure the cell...
cell.textLabel.text = #"abcdefghijklmnopqrstuvwxyz";
float xx = cell.cellButton.frame.origin.x + cell.cellButton.frame.size.width + 5;
CGRect frame = cell.textLabel.frame;
frame.origin.x += xx;
cell.textLabel.frame = frame;
return cell;
}
The result being that the button appears on the left side of the cell (as intended), but the first letter I see in the label is 'g', so the first 6 chars are hidden behind the button (not intended).
When I dump the values in the frame, all except origin.x are zero in both methods.
This should work, yes? no? why not? etc.
Thank you all very much!
:bp:
Set frame for your UILabel and UIButton in layoutSubviews method of your custom UITableViewCell
See Apple Documentation on the subject:
Subclasses can override this method as needed to perform more precise
layout of their subviews. You should override this method only if the
autoresizing and constraint-based behaviors of the subviews do not
offer the behavior you want. You can use your implementation to set
the frame rectangles of your subviews directly.
You can create a custom UITableViewCell, access objects in the class or implement methods.
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
#property (nonatomic, retain) UILabel *customCellLabel;
#property (nonatomic, retain) UIButton *customCellButton;
#end
CustomCell.m
#import "TLCell.h"
#implementation CustomCell
#synthesize customCellLabel, customCellButton
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
customCellLabel = [[UILabel alloc] init];
customCellLabel.frame = CGRectMake(180, 0, 60, 30);
[self addSubview:customCellLabel];
customCellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
customCellButton.frame = CGRectMake(0, 0, 60, 30);
[self addSubview:customCellButton];
}
return self;
}
#end
CustomTableView.m
/* --- */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell.customLabel setText:#"hello"];
// [cell.
[cell.customButton addTarget:self action:#selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside];
}
/* --- */
I am wondering what is the best way to make a UITextField fit a UITableView cell.
I have used this method before:
#implementation UITextField (custom)
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 0, bounds.origin.y + 10,
bounds.size.width - 30, bounds.size.height - 16);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
#end
But this causes an issue:
Category is implementing a method which will also be implemented by its primary class
Although I have seen ways to hide these warnings, it feels like this is more of a hack than a correct method.
What is the best way to fit a UItextField to a cell. This is my field:
// Username field
usernameField = [[UITextField alloc] initWithFrame:(CGRectMake(10, 0, 300, 43))];
usernameField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
It is input into the cell like this:
if (indexPath.row == 0) {
[cell.contentView addSubview:usernameField];
}
Image:
I'd suggest doing this by overriding your custom UITableViewCell's layoutSubviews method. Much simpler there imo. Something like:
- (void)layoutSubviews
{
[super layoutSubviews];
float w, h;
w = (int)(self.frame.size.width - 30);
h = (int)(self.frame.size.height - 16);
[self.detailTextLabel setFrame:CGRectMake(0, 10, w, h)];
}
Here's a fragment of the custom cell initialization
#interface MyCustomCell: UITableViewCell {
}
#end
#implementation MyCustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
}
return self;
}
Here's a fragment of where the custom cell would be created:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}