i have implemented this code, its showing both labels, but button not coming in cell tell me the issue
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ImageOnRightCell";
UILabel *mainLabel, *secondLabel;
UIButton *buttonMe;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont systemFontOfSize:14.0];
mainLabel.textAlignment = UITextAlignmentRight;
mainLabel.textColor = [UIColor blackColor];
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];
secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0, 220.0, 25.0)];
secondLabel.tag = SECONDLABEL_TAG;
secondLabel.font = [UIFont systemFontOfSize:12.0];
secondLabel.textAlignment = UITextAlignmentRight;
secondLabel.textColor = [UIColor darkGrayColor];
secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:secondLabel];
buttonMe = [[UIButton alloc] initWithFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
buttonMe.tag = PHOTO_TAG;
buttonMe.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:buttonMe];
}
else {
mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
buttonMe = (UIButton *)[cell.contentView viewWithTag:PHOTO_TAG];
}
mainLabel.text = #"main text";
secondLabel.text = #"Secon text";
buttonMe.titleLabel.text=#"oh";
return cell;
}
please help for this
I think the problem is with
buttonMe = [[UIButton alloc] initWithFrame:CGRectMake(260.0, 7.0, 30.0, 30.0)];
it will give you back a UIButton of type UIButtonTypeCustom which will by default be transparent/Clear color.
so may be it is getting added on the cell but is not visible, try and change the background color of the button and check if its still not visible.
To create button programatically use this code
UIButton *buttonMe = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonMe addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[buttonMe setTitle:#"Show View" forState:UIControlStateNormal];
buttonMe.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
buttonMe.tag = PHOTO_TAG;
buttonMe.frame = CGRectMake(260.0, 7.0, 30.0, 30.0);
[cell.contentView addSubview:buttonMe];
Note i included the code for setting the action also.When you click it will try the method aMethod: ,so define it also ,otherwise will appear a crash on button click event
Try this, set backgroundColor for buttonMe:buttonMe.backgroundColor = [UIColor grayColor];
and change "buttonMe.titleLabel.text=#"oh"" to "[buttonMe setTitle:#"oh" forState:UIControlStateNormal];"
Related
I am facing problem for UITableViewCell
What I am doing,
Having three table views on 3 different views of same view controller of iPad.
Among which,
on 1st tableview I have cell with content view button and text.
Text+button is showing well when the app loads first time.
But as soon as I rotate the device the button+text disappears.
What I am doing wrong.
Thanks in advance.
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
if (tableView.tag == 1005)
{
if (indexPath.row == 0)
{
circle = [CAShapeLayer layer];
// Make a circular shape
circularPath=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 35, 35) cornerRadius:MAX(35, 35)];
circle.path = circularPath.CGPath;
// Configure the apperence of the circle
circle.fillColor = [UIColor blackColor].CGColor;
circle.strokeColor = [UIColor blackColor].CGColor;
circle.lineWidth = 0;
UIButton* cameraBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,0, 35,35)];
cameraBtn.backgroundcolor = [UIColor redColor];
[cell.contentView addSubview: cameraBtn];
cameraBtn.layer.mask=circle;
UILabel *photoLbl = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 150, 30)];
photoLbl.backgroundColor = [UIColor clearColor];
photoLbl.font = [UIFont fontWithName:#"OpenSans" size:16];
photoLbl.text = #"Take photo";
[cell.contentView addSubview:photoLbl];
[photoLbl release];
[cameraBtn release];
cell.contentView.frame = cell.bounds;
cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
return cell;
}
I searched for all links on SO, but didn't get suitable solution.
Try upgrading your simulator , I had the same problem and it worked fine when I tested it in a real device.
I'm trying to figure out why my Subviews are duplicating when the UITableview scrolls. If I add the Subviews inside the IF statement for reusing the cell it duplicates and reorders the Subviews. If I put the code on the outside it messes up by putting the Subviews on top of one another when scrolling. Below is my code. What am I doing wrong to cause this?
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
UIColor *cellBackgroundColor = [rgbConverter convertColor:#"#202e35"];
cell.backgroundColor = cellBackgroundColor;
// cell.contentView.backgroundColor = color;
if ((indexPath.row == 0) && (indexPath.section == 0)) {
fullName = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
fullName.font = [UIFont fontWithName:#"HelveticaNeue" size:15];
fullName.autocorrectionType = UITextAutocorrectionTypeNo;
[fullName setClearButtonMode:UITextFieldViewModeWhileEditing];
fullName.textColor = [UIColor lightGrayColor];
[fullName setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
fullName.returnKeyType = UIReturnKeyNext;
fullName.background = [UIImage imageNamed:#"textfieldBG.png"];
[fullName setTag:1];
[fullName setText:#""];
[cell.contentView addSubview:fullName];
}
if ((indexPath.row == 1) && (indexPath.section == 0)){
accountNumber = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
accountNumber.font = [UIFont fontWithName:#"HelveticaNeue" size:15];
//accountNumber.placeholder = #"MLGW Account Number";
accountNumber.autocorrectionType = UITextAutocorrectionTypeNo;
[accountNumber setClearButtonMode:UITextFieldViewModeWhileEditing];
accountNumber.keyboardType = UIKeyboardTypeNumberPad;
accountNumber.returnKeyType = UIReturnKeyNext;
accountNumber.textColor = [UIColor lightGrayColor];
[accountNumber setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
[accountNumber setTag:2];
[accountNumber setText:#""];
accountNumber.background = [UIImage imageNamed:#"textfieldBG.png"];
[cell.contentView addSubview:accountNumber];
}
if ((indexPath.row == 2) && (indexPath.section == 0)){
address = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
address.font = [UIFont fontWithName:#"HelveticaNeue" size:15];
address.placeholder = #"Address";
address.autocorrectionType = UITextAutocorrectionTypeNo;
[address setClearButtonMode:UITextFieldViewModeWhileEditing];
address.keyboardType = UIKeyboardTypeDefault;
address.returnKeyType = UIReturnKeyNext;
address.textColor = [UIColor lightGrayColor];
[address setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
[address setTag:3];
[address setText:#""];
address.background = [UIImage imageNamed:#"textfieldBG.png"];
[cell.contentView addSubview:address];
}
if ((indexPath.row == 3) && (indexPath.section == 0)){
aptNumber = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
aptNumber.font = [UIFont fontWithName:#"HelveticaNeue" size:15];
aptNumber.placeholder = #"Apartment Number";
aptNumber.autocorrectionType = UITextAutocorrectionTypeNo;
aptNumber.returnKeyType = UIReturnKeyNext;
[aptNumber setClearButtonMode:UITextFieldViewModeWhileEditing];
aptNumber.keyboardType = UIKeyboardTypeNamePhonePad;
aptNumber.textColor = [UIColor lightGrayColor];
[aptNumber setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
[aptNumber setTag:4];
[aptNumber setText:#""];
aptNumber.background = [UIImage imageNamed:#"textfieldBG.png"];
[cell.contentView addSubview:aptNumber];
}
if ((indexPath.row == 4) && (indexPath.section == 0)){
city = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 180, 25)];
city.font = [UIFont fontWithName:#"HelveticaNeue" size:15];
city.placeholder = #"City";
city.autocorrectionType = UITextAutocorrectionTypeNo;
city.returnKeyType = UIReturnKeyNext;
[city setClearButtonMode:UITextFieldViewModeWhileEditing];
city.keyboardType = UIKeyboardTypeDefault;
city.textColor = [UIColor lightGrayColor];
[city setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
[city setTag:5];
[city setText:#""];
city.background = [UIImage imageNamed:#"textfieldBG.png"];
[cell.contentView addSubview:city];
zip = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+210, cell.frame.origin.y+10,80, 25)];
zip.font = [UIFont fontWithName:#"HelveticaNeue" size:15];
zip.placeholder = #"Zip";
zip.autocorrectionType = UITextAutocorrectionTypeNo;
zip.returnKeyType = UIReturnKeyNext;
[zip setClearButtonMode:UITextFieldViewModeWhileEditing];
zip.keyboardType = UIKeyboardTypeNumberPad;
zip.textColor = [UIColor lightGrayColor];
[zip setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
[zip setTag:6];
[zip setText:#""];
zip.background = [UIImage imageNamed:#"textfieldBG.png"];
[cell.contentView addSubview:zip];
}
if ((indexPath.row == 5) && (indexPath.section == 0)){
phoneNbr = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
phoneNbr.font = [UIFont fontWithName:#"HelveticaNeue" size:15];
phoneNbr.placeholder = #"Phone Number";
phoneNbr.autocorrectionType = UITextAutocorrectionTypeNo;
phoneNbr.returnKeyType = UIReturnKeyNext;
[phoneNbr setClearButtonMode:UITextFieldViewModeWhileEditing];
phoneNbr.keyboardType = UIKeyboardTypeNumberPad;
phoneNbr.textColor = [UIColor lightGrayColor];
[phoneNbr setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
[phoneNbr setTag:7];
[phoneNbr setText:#""];
phoneNbr.background = [UIImage imageNamed:#"textfieldBG.png"];
[cell.contentView addSubview:phoneNbr];
}
if ((indexPath.row == 6) && (indexPath.section == 0)){
emailAddress = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
emailAddress.font = [UIFont fontWithName:#"HelveticaNeue" size:15];
emailAddress.placeholder = #"Email Address";
emailAddress.returnKeyType = UIReturnKeyNext;
emailAddress.autocorrectionType = UITextAutocorrectionTypeNo;
[emailAddress setClearButtonMode:UITextFieldViewModeWhileEditing];
emailAddress.keyboardType = UIKeyboardTypeEmailAddress;
emailAddress.textColor = [UIColor lightGrayColor];
[emailAddress setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
[emailAddress setTag:8];
[emailAddress setText:#""];
emailAddress.background = [UIImage imageNamed:#"textfieldBG.png"];
[cell.contentView addSubview:emailAddress];
}
if ((indexPath.row == 7) && (indexPath.section == 0)){
validateEmail = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
validateEmail.font = [UIFont fontWithName:#"HelveticaNeue" size:15];
validateEmail.placeholder = #"Re-enter Email Address";
validateEmail.returnKeyType = UIReturnKeyNext;
validateEmail.autocorrectionType = UITextAutocorrectionTypeNo;
[validateEmail setClearButtonMode:UITextFieldViewModeWhileEditing];
validateEmail.keyboardType = UIKeyboardTypeEmailAddress;
validateEmail.textColor = [UIColor lightGrayColor];
[validateEmail setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
[validateEmail setTag:9];
[validateEmail setText:#""];
validateEmail.background = [UIImage imageNamed:#"textfieldBG.png"];
[cell.contentView addSubview:validateEmail];
}
if ((indexPath.row == 0) && (indexPath.section == 1)){
oneTimeDonationBtn = [[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 38, 32)];
[oneTimeDonationBtn setImage:[UIImage imageNamed:#"radioBox.png"] forState:UIControlStateNormal];
[oneTimeDonationBtn addTarget:self action:#selector(oneTimeAmountAction:) forControlEvents:UIControlEventTouchUpInside];
oneTimeDonation = [[UITextField alloc] initWithFrame:CGRectMake(oneTimeDonationBtn.frame.origin.x+35, cell.frame.origin.y+13, 150, 25)];
oneTimeDonation.font = [UIFont fontWithName:#"HelveticaNeue" size:15];
oneTimeDonation.placeholder = #"One Time Donation";
oneTimeDonation.autocorrectionType = UITextAutocorrectionTypeNo;
oneTimeDonation.returnKeyType = UIReturnKeyNext;
[oneTimeDonation setClearButtonMode:UITextFieldViewModeWhileEditing];
oneTimeDonation.keyboardType = UIKeyboardTypeDecimalPad;
oneTimeDonation.textColor = [UIColor lightGrayColor];
[oneTimeDonation setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
[oneTimeDonation setTag:10];
[oneTimeDonation setText:#""];
oneTimeDonation.background = [UIImage imageNamed:#"textfieldBG.png"];
[cell.contentView addSubview:oneTimeDonationBtn];
[cell.contentView addSubview:oneTimeDonation];
}
if ((indexPath.row == 0) && (indexPath.section == 2)){
oneDollarBtn = [[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 38, 32)];
[oneDollarBtn setImage:[UIImage imageNamed:#"radioBox.png"] forState:UIControlStateNormal];
[oneDollarBtn addTarget:self action:#selector(oneDollarAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:oneDollarBtn];
oneDollarLbl = [[UILabel alloc ] initWithFrame:CGRectMake(oneDollarBtn.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
oneDollarLbl.text = #"$1";
oneDollarLbl.textColor = [UIColor lightGrayColor];
[cell.contentView addSubview:oneDollarLbl];
fiveDollarBtn = [[UIButton alloc]initWithFrame:CGRectMake(oneDollarLbl.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
[fiveDollarBtn setImage:[UIImage imageNamed:#"radioBox.png"] forState:UIControlStateNormal];
[fiveDollarBtn addTarget:self action:#selector(fiveDollarAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:fiveDollarBtn];
fiveDollarLbl = [[UILabel alloc ] initWithFrame:CGRectMake(fiveDollarBtn.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
fiveDollarLbl.text = #"$5";
fiveDollarLbl.textColor = [UIColor lightGrayColor];
[cell.contentView addSubview:fiveDollarLbl];
tenDollarBtn = [[UIButton alloc]initWithFrame:CGRectMake(fiveDollarLbl.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
[tenDollarBtn setImage:[UIImage imageNamed:#"radioBox.png"] forState:UIControlStateNormal];
[tenDollarBtn addTarget:self action:#selector(tenDollarAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:tenDollarBtn];
tenDollarLbl = [[UILabel alloc ] initWithFrame:CGRectMake(tenDollarBtn.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
tenDollarLbl.text = #"$10";
tenDollarLbl.textColor = [UIColor lightGrayColor];
[cell.contentView addSubview:tenDollarLbl];
}
if ((indexPath.row == 1) && (indexPath.section == 2)){
otherAmountBtn = [[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 38, 32)];
[otherAmountBtn setImage:[UIImage imageNamed:#"radioBox.png"] forState:UIControlStateNormal];
[otherAmountBtn addTarget:self action:#selector(otherAmountAmountAction:) forControlEvents:UIControlEventTouchUpInside];
otherAmount = [[UITextField alloc] initWithFrame:CGRectMake(otherAmountBtn.frame.origin.x+35, cell.frame.origin.y+13, 150, 25)];
otherAmount.font = [UIFont fontWithName:#"HelveticaNeue" size:15];
otherAmount.placeholder = #"Other Amount";
otherAmount.autocorrectionType = UITextAutocorrectionTypeNo;
otherAmount.returnKeyType = UIReturnKeyNext;
[otherAmount setClearButtonMode:UITextFieldViewModeWhileEditing];
otherAmount.keyboardType = UIKeyboardTypeDecimalPad;
otherAmount.textColor = [UIColor lightGrayColor];
[otherAmount setValue:[UIColor lightGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
[otherAmount setTag:10];
[otherAmount setText:#""];
otherAmount.background = [UIImage imageNamed:#"textfieldBG.png"];
[cell.contentView addSubview:otherAmountBtn];
[cell.contentView addSubview:otherAmount];
}
if ((indexPath.row == 0) && (indexPath.section == 3)){
comments = [[UITextView alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 280, 100)];
comments.font = [UIFont fontWithName:#"HelveticaNeue" size:15];
comments.autocorrectionType = UITextAutocorrectionTypeNo;
comments.keyboardType = UIKeyboardTypeDefault;
comments.textColor = [UIColor lightGrayColor];
[comments setTag:11];
[comments setText:#""];
comments.contentInset = UIEdgeInsetsMake(2.0, 1.0, 0.0, 0.0);
[cell.contentView addSubview:comments];
}
}
//cell.imageView.image = [UIImage imageNamed:#"settings_icon.png"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
return cell;
}
This happens when you are using only ONE cellIdentifier to reuse different kind of cells by invoke: dequeueReusableCellWithIdentifier method.
In your code, you are adding different view at different indexPath, but you don't know what cell is given by dequeueReusableCellWithIdentifier method.
For example, when you add address view to a reused cell, it could already have a fullName subview on it. Then you will get an unexpected result.
There are many ways to solve the problem.
Here are two that I think is easy for your situation, you can chose any one you like:
Since your cell contents are not so "reusable", if the performance is not a problem, I recommend you not to use dequeueReusableCellWithIdentifier at all.
You'd better use different cellIdentifier for different kind of cell.
The main reason is that dequeueReusableCellWithIdentifier, as the name suggests, gets a cell already created regardless of order. I guess it caches only as many cells as visible ones.
When you scroll up, it gives you a created (and filled) cell.
Replace the cell creation by this:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
Also remove the if (cell == nil). Also remove the other cell creation
Remember that dequeueReusableCellWithIdentifier:forIndexPath: never returns nil. So it is a good idea to check if the cell was already filled to skip the subviews creation.
Your code is a little hard to go through.
Every time you have to customize the cell a lot , then you should not use UITableViewCell, instead you should subclass it, and customize it in your custom class.
In my experience I find if statement complex when both section and rows are involved.
I suggest you to use switch statement, where you can provide logic for each case.
This will make your life easier, something along the lines:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
//fullName custom cell
break;
case 1:
//account number custom cell
break;
default:
break;
}
break;
default:
break;
}
return cell;
}
First set textfield tag to some constant, you can fetch text fields by using cell indexPath.Secondly add textfield if its not already there like below;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//check if you already have textfield, say a constant tag is 100
UITextField *myTextField ;
if(cell){
myTextField = (UITextField *)[cell.contentView viewWithTag:100];
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//now if myTextField is nil then only init it
if(!myTextField){
//Configure textfield here and add to cell content view
}
}
//now all things are configure for cell , assign whatever text you want to myTextField
if(indexPath.row == 0 && indexPath.section == 0){
//.......
[myTextField setText:#"XYZ"];
}
}
I have a dynamically set table view controller that is used to populate a news type feed. I am having trouble connecting one class to the other for setting the text, image, etc... of that current cell.
Here is the gist:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"timelineCell";
FBGTimelineCell *cell = (FBGTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.text = [self.googlePlacesArrayFromAFNetworking[indexPath.row] objectForKey:#"message"]; <!-- here is where I want to for example, set the title to the message within this array.
if (!cell)
{
cell = [[FBGTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell initTimelineCell];
}
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg", indexPath.row]];
cell.photoView.image = img;
return cell;
}
Here is the FBH... init function:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
cellContentView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 300, 440)];
cellContentView.backgroundColor = [UIColor whiteColor];
photoView = [[FBGTimelinePhotoView alloc] initWithFrame:CGRectMake(5, 102, 310, 310)];
photoView.backgroundColor = [UIColor darkGrayColor];
[photoView setUserInteractionEnabled:YES];
UIView *profilePic = [[UIView alloc] initWithFrame:CGRectMake(9, 9, 30, 30)];
profilePic.backgroundColor = [UIColor darkGrayColor];
UILabel *usernameLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 9, 222, 18)];
usernameLabel.font = [UIFont systemFontOfSize:14.0];
usernameLabel.text = #"Username";
UILabel *timestampLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 25, 222, 17)];
timestampLabel.font = [UIFont systemFontOfSize:12.0];
timestampLabel.text = #"3 hours ago";
timestampLabel.textColor = [UIColor lightGrayColor];
UILabel *statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 50, 283, 41)];
statusLabel.font = [UIFont systemFontOfSize:15.0];
statusLabel.text = #"Status..status..status..status..status..status..status..status..status..status..status..status..status..status..status..";
statusLabel.numberOfLines = 2;
statusLabel.lineBreakMode = NSLineBreakByWordWrapping;
UILabel *likeLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 413, 32, 21)];
likeLabel.font = [UIFont systemFontOfSize:13.0];
likeLabel.text = #"Like";
UILabel *commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(113, 413, 74, 21)];
commentLabel.font = [UIFont systemFontOfSize:13.0];
commentLabel.text = #"Comment";
UILabel *shareLabel = [[UILabel alloc] initWithFrame:CGRectMake(246, 413, 46, 21)];
shareLabel.font = [UIFont systemFontOfSize:13.0];
shareLabel.text = #"Share";
[self addSubview:cellContentView];
[self addSubview:photoView];
[cellContentView addSubview:profilePic];
[cellContentView addSubview:usernameLabel];
[cellContentView addSubview:timestampLabel];
[cellContentView addSubview:statusLabel];
[cellContentView addSubview:likeLabel];
[cellContentView addSubview:commentLabel];
[cellContentView addSubview:shareLabel];
}
return self;
}
So I need to be able to set the cell text and different views within the cell within the cellForRowAtIndexPath if possible for each cell that is set up in the FB..init function.
Suggestions, thoughts?
You should create properties (in the FBGTimelineCell class) for each of the UI elements that you need to access in cellFroRowAtIndexPath, then you can access them with cell.propertyName.
You can create properties for all the dynamic values which are expected to be changed with each cell. Those properties you can set in your cellForRowAtIndexPath method.
eg. cell.statusText = [NSString stringWithFormat:#"%# is rocking!!", indexPath.row];
Public properties like statusText you can add all the UIElements expecting to be dynamically added.
Hope it helps.
Cheers.
I need to create and populate custom cells. These cells height are huge about 300-400.
So I have a populateCellView function , I call that function on heightForRowAtIndexPath to calculate height and in cellForRowAtIndexPath to add that view to cell content view, but somehow sometimes cells overlap.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [self populateCellView:indexPath.row].frame.size.height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *populateCell= nil;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
populateCell=[[UIView alloc] initWithFrame:CGRectZero];
[[populateCell layer] setBorderWidth:2.0f];
populateCell= [self populateCellView:indexPath.row];
[[cell contentView] addSubview:populateCell];
}
if (!populateCell){
populateCell = (UIView*)[cell viewWithTag:1];
populateCell= [self populateCellView:indexPath.row];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
populate view function:
-(UIView *)populateCellView:(int)forCase
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,500)];
[view setBackgroundColor:[UIColor colorWithRed:40/255.0 green:150/255.0 blue:213/255.0 alpha:1.0]];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
if ([view subviews]){
for (UIView *subview in [view subviews]) {
[subview removeFromSuperview];
}
}
UILabel *caseNumberLabel;
UILabel *caseNameLabel;
UILabel *caseSummaryLabel;
UILabel *caseAncLabel;
UILabel *caseNumberText;
UILabel *caseNameText;
UILabel *caseSummaryText;
UILabel *caseAncText;
//static label for case number
caseNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,10,250, 50)];
caseNumberLabel.textColor = [UIColor whiteColor];
caseNumberLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:24.0f];
caseNumberLabel.text =#"Case Number:";
caseNumberLabel.backgroundColor = [UIColor clearColor];
caseNumberLabel.numberOfLines=0;
caseNumberLabel.adjustsFontSizeToFitWidth = NO;
caseNumberLabel.tag=forCase;
caseNumberLabel.frame=[self calculateLabelFrame:caseNumberLabel];
//case number from plist
caseNumberText = [[UILabel alloc] initWithFrame:CGRectMake(caseNumberLabel.frame.size.width+50,caseNumberLabel.frame.origin.y,self.view.frame.size.width-caseNumberLabel.frame.size.width-50, 50)];
caseNumberText.textColor = [UIColor whiteColor];
caseNumberText.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:24.0f];
caseNumberText.text =[[self agenda] getCaseNumber:forCase];
caseNumberText.backgroundColor = [UIColor clearColor];
caseNumberText.numberOfLines=0;
caseNumberText.adjustsFontSizeToFitWidth = NO;
caseNumberText.tag=forCase;
//[caseNumberLabel sizeToFit];
caseNumberText.frame=[self calculateLabelFrame:caseNumberText];
//static label for case name
caseNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,caseNumberText.frame.origin.y + caseNumberText.bounds.size.height+5,250, 50)];
caseNameLabel.textColor =[UIColor whiteColor];
caseNameLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:24.0f];
caseNameLabel.text =#"Case Name:";
caseNameLabel.backgroundColor = [UIColor clearColor];
caseNameLabel.numberOfLines=0;
caseNameLabel.adjustsFontSizeToFitWidth = NO;
caseNameLabel.tag=forCase;
//[caseNumberLabel sizeToFit];
caseNameLabel.frame=[self calculateLabelFrame:caseNameLabel];
//case name from plist
caseNameText = [[UILabel alloc] initWithFrame:CGRectMake(caseNameLabel.frame.size.width+50,caseNameLabel.frame.origin.y,self.view.frame.size.width-caseNameLabel.frame.size.width-50, 50)];
caseNameText.textColor = [UIColor whiteColor];
caseNameText.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:24.0f];
caseNameText.text =[[self agenda] getCaseName:forCase];
caseNameText.backgroundColor = [UIColor clearColor];
caseNameText.numberOfLines=0;
//caseNameText.adjustsFontSizeToFitWidth = NO;
caseNameText.tag=forCase;
[caseNameText sizeToFit];
//caseNameText.lineBreakMode = NSLineBreakByWordWrapping;
caseNameText.frame=[self calCellLabelFrame:caseNameText previousLabel:caseNameLabel];
//static label for case summary
caseSummaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,caseNameText.frame.origin.y + caseNameText.bounds.size.height+20,250, 50)];
caseSummaryLabel.textColor = [UIColor whiteColor];
caseSummaryLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:24.0f];
caseSummaryLabel.text =#"Case Summary:";
caseSummaryLabel.backgroundColor = [UIColor clearColor];
caseSummaryLabel.numberOfLines=0;
caseSummaryLabel.adjustsFontSizeToFitWidth = NO;
caseSummaryLabel.tag=forCase;
//[caseSummaryLabel sizeToFit];
caseSummaryLabel.frame=[self calculateLabelFrame:caseSummaryLabel];
//case name from plist
caseSummaryText = [[UILabel alloc] initWithFrame:CGRectMake(caseSummaryLabel.frame.size.width+50,caseSummaryLabel.frame.origin.y,self.view.frame.size.width-caseSummaryLabel.frame.size.width-100, 50)];
caseSummaryText.textColor = [UIColor whiteColor];
caseSummaryText.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:24.0f];
caseSummaryText.text =[[self agenda] getCaseSummary:forCase];
caseSummaryText.backgroundColor = [UIColor clearColor];
caseSummaryText.numberOfLines=0;
//caseSummaryText.adjustsFontSizeToFitWidth = NO;
caseSummaryText.tag=forCase;
[caseSummaryText sizeToFit];
//caseSummaryText.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
caseSummaryText.frame=[self calCellLabelFrame:caseSummaryText previousLabel:caseSummaryLabel];
//static label for anc
caseAncLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,caseSummaryText.frame.origin.y + caseSummaryText.bounds.size.height+15,250, 50)];
caseAncLabel.textColor = [UIColor whiteColor];
caseAncLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:24.0f];
caseAncLabel.text =#"ANC:";
caseAncLabel.backgroundColor = [UIColor clearColor];
caseAncLabel.numberOfLines=0;
caseAncLabel.adjustsFontSizeToFitWidth = NO;
caseAncLabel.tag=forCase;
//[ccaseAncLabel sizeToFit];
caseAncLabel.frame=[self calculateLabelFrame:caseAncLabel];
//case name from plist
caseAncText = [[UILabel alloc] initWithFrame:CGRectMake(caseAncLabel.frame.size.width+50,caseAncLabel.frame.origin.y,self.view.frame.size.width-caseAncLabel.frame.size.width-50, 50)];
caseAncText.textColor = [UIColor whiteColor];
caseAncText.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:24.0f];
caseAncText.text =[[self agenda] getCaseAnc:forCase];
caseAncText.backgroundColor = [UIColor clearColor];
caseAncText.numberOfLines=0;
//caseAncText.adjustsFontSizeToFitWidth = NO;
caseAncText.tag=forCase;
[caseAncText sizeToFit];
//caseSummaryText.lineBreakMode = NSLineBreakByWordWrapping;
caseAncText.frame=[self calCellLabelFrame:caseAncText previousLabel:caseAncLabel];
//add button
UIButton *acceptButton=[UIButton buttonWithType:UIButtonTypeCustom];
[acceptButton setTag:forCase];
acceptButton.titleLabel.tag=1;
[acceptButton setFrame:CGRectMake(caseAncLabel.frame.size.width+50,caseAncText.frame.origin.y + caseAncText.bounds.size.height+20,181,39)];
NSString *radioButtonImage=#"view_attachment.png";
UIImage *acceptbuttonImage = [UIImage imageNamed:radioButtonImage];
[acceptButton setBackgroundImage:acceptbuttonImage forState:UIControlStateNormal];
[acceptButton addTarget:self action:#selector(showAttachements:) forControlEvents:UIControlEventTouchUpInside];
//whie line
UIView *anotherline = [[UIView alloc] initWithFrame:CGRectMake(0, acceptButton.frame.origin.y + acceptButton.bounds.size.height+15, self.view.frame.size.width, 1)];
anotherline.backgroundColor=[UIColor whiteColor];
anotherline.tag=forCase;
[view addSubview:acceptButton];
[view addSubview: caseNumberLabel];
[view addSubview:caseNumberText];
[view addSubview:caseNameLabel];
[view addSubview:caseNameText];
[view addSubview:caseSummaryLabel];
[view addSubview:caseSummaryText];
[view addSubview:caseAncText];
[view addSubview:caseAncLabel];
[view addSubview:anotherline];
view.frame = CGRectMake(0,0, self.view.frame.size.width,anotherline.frame.origin.y + anotherline.bounds.size.height+5);
CGFloat redLevel = rand() / (float) RAND_MAX;
CGFloat greenLevel = rand() / (float) RAND_MAX;
CGFloat blueLevel = rand() / (float) RAND_MAX;
view.backgroundColor = [UIColor colorWithRed: redLevel
green: greenLevel
blue: blueLevel
alpha: 1.0];
return view;
}
I call reload data like this
dispatch_async(dispatch_get_main_queue(), ^{
[_agenda loadFromPlist];
[[self tableView] reloadData];
});
Any idea why cells overlaps?
Its your UITableview cellforIndexPath causing overlap
try this
if (!populateCell){
populateCell = (UIView*)[cell viewWithTag:1];
populateCell= [self populateCellView:indexPath.row];
if ([[cell contentView] subviews]){
for (UIView *subview in [[cell contentView] subviews]) {
[subview removeFromSuperview];
}
}
[[cell contentView] addSubview:populateCell];
}
cell.backgroundColor=[UIColor clearColor];
At the end of the UITableViewDataSource method, tableView:cellForRowAtIndexPath:, you have to call [[cell contentView] addSubView:populateCell]. If the table dequeued a cell successfully, you have to retrieve the appropriate populateCell and add it to the contentView.
If you use storyboards, and set a different height for the cells, and the rows in the tableview, the height will usually overlap. The other answers are also highly valid, but usually when I've encountered this problem, the bug lies in a different value on row height and cell height in the tableview object in your storyboard...
I have a table going where each cell has a UIView in it that is fed from an XMLParser feed. If I leave it how it is, each time I refresh the views build upon each other because they are not getting released/removed, which makes total sense. How would I go about writing my code to avoid that problem, and not having to rebuild the view's each time I refresh. I am stuck in the logic of it and also the actual syntax. Thanks.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"texture.png"]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIView *selectionView = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 200, 65)];
[selectionView setBackgroundColor: [UIColor clearColor]];
cell.selectedBackgroundView = selectionView;
}
// Display content in each cell
cellSeparator = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 300, 65)];
[cellSeparator setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth];
[cellSeparator setContentMode:UIViewContentModeTopLeft];
[cellSeparator setBackgroundColor: [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0]];
cellSeparator.layer.borderWidth = 1.0;
cellSeparator.layer.borderColor = [UIColor blackColor].CGColor;
[cell addSubview:cellSeparator];
callTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 2, 190, 21)];
callTypeLabel.text = [currentCall currentCallType];
callTypeLabel.backgroundColor = [UIColor clearColor];
callTypeLabel.font = [UIFont boldSystemFontOfSize:12.0];
[cellSeparator addSubview:callTypeLabel];
locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 17 , 190, 15)];
locationLabel.text = [currentCall location];
locationLabel.backgroundColor = [UIColor clearColor];
locationLabel.font = [UIFont systemFontOfSize:10.0];
[cellSeparator addSubview:locationLabel];
unitsLabel = [[UILabel alloc]initWithFrame:CGRectMake(4, 43, 190, 21)];
unitsLabel.text = [currentCall units];
unitsLabel.backgroundColor = [UIColor clearColor];
unitsLabel.font = [UIFont italicSystemFontOfSize:10.0];
[cellSeparator addSubview:unitsLabel];
stationLabel = [[UILabel alloc]initWithFrame:CGRectMake(195 , 25, 75, 20)];
NSString *station = [#"Station: " stringByAppendingString:currentCall.station];
stationLabel.text = station;
stationLabel.backgroundColor = [UIColor clearColor];
stationLabel.font = [UIFont boldSystemFontOfSize:11.0];
[cellSeparator addSubview:stationLabel];
if ([currentCall.county isEqualToString:#"W"]) {
UIImage *countyImage = [UIImage imageNamed:#"blue.png"];
CGRect countyImageFrame = CGRectMake(275, 10, 18, 18);
UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame];
countyImageLabel.image = countyImage;
[cellSeparator addSubview:countyImageLabel];
} else {
UIImage *countyImage = [UIImage imageNamed:#"green.png"];
CGRect countyImageFrame = CGRectMake(275, 10, 18, 18);
UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame];
countyImageLabel.image = countyImage;
[cellSeparator addSubview:countyImageLabel];
}
if ([currentCall.callType isEqualToString:#"F"]) {
UIImage *callTypeImage = [UIImage imageNamed:#"red.png"];
CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageLabel.image = callTypeImage;
[cellSeparator addSubview:callTypeImageLabel];
} else {
UIImage *callTypeImage = [UIImage imageNamed:#"yellow.png"];
CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageLabel.image = callTypeImage;
[cellSeparator addSubview:callTypeImageLabel];
}
return cell;
}
If I understand your question correctly, I think the solution you want is to subclass UITableViewCell, set up your subviews (UIImageViews and all that) and make them properties on your subclass. Then you can
if (!cell.imageView) {
cell.imageView = [[UIImageView alloc] initWithFrame:myFrame];
}
then just set the image. That way you're sure to not stack image views on top of one another and the correct image will get set when the cell is dequeued.
EDIT:
Make a class called MyTableViewCell (or whatever you want to call it but make sure it's a subclass of UITableViewCell)
Add #property (strong, nonatomic) UIImageView *imageView;
Then use MyTableViewCell instead of UITableViewCell in you cellForRowAtIndexPath and you can access the image property of your custom table view cell, check if it exists, if not create it (which is what the above code does) and then set the image. I would give you code but I'm on my phone. Google subclassing UITableviewCell. You should find a ton of examples.