UIButtons & UILabels inside a tableviewcell - ios

I have two UIButtons and some labels inside a table view cell. I have 2 sections, the first section contains one table view cell and inside of that cell there are two UIButtons that call on an api to fetch new data for the cells in section 2. In a certain if case, a label is created because the string is very long so I have to create a new frame for the cell.
The problem that I am currently having is that the button text also gets update with new information and sometimes the string is short or sometimes the string is long, so I have to remove the button and a add a new button with a new frame to fit the new string. But if the past button string was shorter than the new one, the button still appears in the background. I tried removing it from the superview and also checking that it has removed from the superview (check that it is null once I remove it) and it is, but it still appears on the screen. The same thing happens with the UILabels as well. I also tried to removing them from super view with a tag (viewWithTag:123). Anyone have any pointers, ideas, suggestions, anything really, that could help me figure/fix this out?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
cell.selectedBackgroundView = selectedBackgroundView;
if(cell==nil) { NSLog(#"Cell is still nil");}
}
cell.textLabel.text = nil;
cell.accessoryView = nil;
cell.detailTextLabel.text = nil;
cell.userInteractionEnabled = YES;
if(indexPath.section == 0)
{
dosageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
amountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[dosageButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[dosageButton setContentEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
[amountButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[amountButton setContentEdgeInsets:UIEdgeInsetsMake(0, 15.5, 0, 0)];
if(strengths.count == 0) { [dosageButton setTitle:#"Dosage" forState:UIControlStateNormal];}
else
{
[dosageButton setTitle:selectedStrength forState:UIControlStateNormal];
CGSize stringsize = [selectedStrength sizeWithFont:[UIFont systemFontOfSize:14]];
[dosageButton setFrame:CGRectMake(5, 27.5, stringsize.width + 30, 30)];
}
if(quantity.count == 0) { [amountButton setTitle:#"Amount" forState:UIControlStateNormal];}
else
{
int value = [selectedQuantity intValue];
if (value == 1)
{
NSString *amountAndFormat = [NSString stringWithFormat:#"%# %#", selectedQuantity, selectedFormat];
[amountButton setTitle:amountAndFormat forState:UIControlStateNormal];
CGSize stringsize = [selectedStrength sizeWithFont:[UIFont systemFontOfSize:14]];
CGSize amountSize = [amountAndFormat sizeWithFont:[UIFont systemFontOfSize:14]];
if(amountSize.width+stringsize.width > 250)
{
dosageButton.frame = CGRectMake(5, 27.5, 130, 30);
amountButton.frame = CGRectMake(140, 27.5, 160, 30);
}
else
{
[amountButton setFrame:CGRectMake(stringsize.width + 40, 27.5, amountSize.width + (amountSize.height * 2.1), 30)];
}
}
else
{
NSString *amountAndFormat = [NSString stringWithFormat:#"%# %#", selectedQuantity, selectedFormatPlural];
[amountButton setTitle:amountAndFormat forState:UIControlStateNormal];
CGSize stringsize = [selectedStrength sizeWithFont:[UIFont systemFontOfSize:14]];
CGSize amountSize = [amountAndFormat sizeWithFont:[UIFont systemFontOfSize:14]];
if(amountSize.width + stringsize.width> 250)
{
dosageButton.frame = CGRectMake(5, 27.5, 130, 30);
amountButton.frame = CGRectMake(140, 27.5, 160, 30);
}
else
{
[amountButton setFrame:CGRectMake(stringsize.width + 40, 27.5, amountSize.width + (amountSize.height * 2.1), 30)];
}
}
}
[dosageButton addTarget:self action:#selector(showDosages:) forControlEvents:UIControlEventTouchUpInside];
[amountButton addTarget:self action:#selector(showAmount:) forControlEvents:UIControlEventTouchUpInside];
//[cell.contentView addSubview:dosageButton];
//[cell.contentView addSubview:amountButton];
//adding it to view instead of cell because of userInteraction.
//if I add it to cell.contentView and userInteractionEnabled = NO
//then the user can't press the button
//but if the it is enabled then they can click the cell and looks tacky!
[self.view addSubview:dosageButton];
[self.view addSubview:amountButton];
cell.userInteractionEnabled = NO;
}
if(indexPath.section == 1)
{
if(localName.count == 0)
{
//Nothing
}
else
{
CGRect longStringFrame = CGRectMake(12, 4, 250, 120);
//UILabel *longStringLabel = [[UILabel alloc] initWithFrame:longStringFrame];
UILabel *longStringLabel = [[UILabel alloc] initWithFrame:longStringFrame];
longStringLabel.backgroundColor = [UIColor clearColor];
longStringLabel.font = [UIFont systemFontOfSize:12];
longStringLabel.text = [NSString stringWithFormat:#"Something super duper long. Some really really long string that has a lot of information and cannot fit in one line so I have to use numberOfLines = 0 and NSLineBreakByWordWrapping. This string is really really really long.]];
longStringLabel.textColor = [UIColor blackColor];
longStringLabel.numberOfLines = 0;
longStringLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.text = #"";
longStringLabel.tag = 123;
[cell.contentView addSubview:longStringLabel];
}
}
}
- (void) showDosages:(UIButton *)sender
{
[dosageButton removeFromSuperview];
[amountButton removeFromSuperview];
[[cell.contentView viewWithTag:123] removeFromSuperview];
NSLog(#"%#",[dosageButton superview]);
NSLog(#"%#",[amountButton superview]);
//Also tried it this way as well. Didn't work
//[dosageButton performSelectorOnMainThread:#selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
//[amountButton performSelectorOnMainThread:#selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
dispatch_async(someDispatch, ^{ NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat: #"http://somewebsite.com"]]]; [self performSelectorOnMainThread:#selector(fetchData:) withObject:data waitUntilDone:YES]; });
}
showAmount is similar to showDosages

I would suggest you to create a custom cell put all the labels & buttons you want, give an iboutles so that you can easily manage things.
Custom cell tutorial
http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/
Once you made your cell you can set the height of cell according to your string. Your buttons will automatically adjusted when your cell size is increased or decreased

Here is the simple example of multiple cell identifier hope it might help
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier1 = #"Cell1";
static NSString *CellIdentifier2 = #"Cell2";
if(flag == 0) {
MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease];
}
cell.textLabel.text= #"Cell1";
return cell;
}
else {
NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2 orientation:currentOrientation] autorelease];
}
cell.textLabel.text= #"Cell1";
return cell;
}
return nil;
}

Related

UITableView Custom cell changing values iOS 9

I am facing an issue in custom cell, while scrolling up and down the values in the cell changing. I have added 2 buttons in the last section. Please check the below code. Any help, much appreciated. Thanks a ton.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
DrinkCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[DrinkCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.section == [tableView numberOfSections] - 1)
{
cell.tileImage.hidden = YES;
cell.pizzaTopins.hidden = YES;
// cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.row == 0) {
cell.separatorInset = UIEdgeInsetsMake(0.f, 0.f, 0.f, cell.bounds.size.width);
UILabel *calorieLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, cell.contentView.frame.size.height/2-30/2, self.tableView.frame.size.width, 30)];
calorieLabel.text = #"1000 Calories | 700 grams of fat";
cell.backgroundColor = backgroundColorApp;
[cell addSubview:calorieLabel];
}
else if (indexPath.row == 1) {
cell.backgroundColor = backgroundColorApp;
NSLog(#"cell width : %f",cell.contentView.frame.size.width);
UIView *quantityView = [[UIView alloc]init];
quantityView.frame = CGRectMake(25 ,cell.contentView.frame.size.height/2-50/2, cell.contentView.frame.size.width-55, 50);
quantityView.backgroundColor = [UIColor whiteColor];
quantityView.clipsToBounds = YES;
quantityView.userInteractionEnabled = YES;
quantityView.layer.cornerRadius = 10.0f;
UIButton *decreaseButton = [[UIButton alloc]initWithFrame:CGRectMake(10, quantityView.frame.size.height/2-35/2, 35, 35)];
[decreaseButton setTitle:#"-" forState:UIControlStateNormal];
[quantityView addSubview:decreaseButton];
[decreaseButton addTarget:self action:#selector(decreaseQuantity:) forControlEvents:UIControlEventTouchUpInside];
[decreaseButton setTitleColor:quantityButtonColor forState:UIControlStateNormal];
[decreaseButton setTintColor:[UIColor greenColor]];
decreaseButton.clipsToBounds = YES;
decreaseButton.layer.cornerRadius = 17.5f;
decreaseButton.layer.borderColor = quantityButtonColor.CGColor;
decreaseButton.layer.borderWidth = 1.5f;
int i =0;
quantityLabel = [[UILabel alloc]initWithFrame:CGRectMake(quantityView.frame.size.width/2-30/2, quantityView.frame.size.height/2-30/2, 30, 30)];
quantityLabel.text = [NSString stringWithFormat:#"%d",i];
[quantityLabel setFont:[UIFont boldSystemFontOfSize:18]];
quantityLabel.textColor = quantityButtonColor;
[quantityView addSubview:quantityLabel];
UIButton *increaseButton = [[UIButton alloc]initWithFrame:CGRectMake(quantityView.frame.size.width-45, quantityView.frame.size.height/2-35/2, 35, 35)];
[increaseButton setTitle:#"+" forState:UIControlStateNormal];
[increaseButton addTarget:self action:#selector(increaseQuantity:) forControlEvents:UIControlEventTouchUpInside];
[quantityView addSubview:increaseButton];
increaseButton.clipsToBounds = YES;
[increaseButton setTitleColor:quantityButtonColor forState:UIControlStateNormal];
increaseButton.layer.cornerRadius = 17.5f;
increaseButton.layer.borderColor = quantityButtonColor.CGColor;
increaseButton.layer.borderWidth = 1.5f;
[cell addSubview:quantityView];
[quantityView addSubview:decreaseButton];
[quantityView addSubview:increaseButton];
}
else if (indexPath.row == 2) {
addTocartButton = [[UIButton alloc]initWithFrame:CGRectMake(25, cell.contentView.frame.size.height/2 - 50/2, cell.contentView.frame.size.width-55, 50)];
[addTocartButton setTitle:#"Add to Cart" forState:UIControlStateNormal];
[addTocartButton addTarget:self action:#selector(addToCartOnCreatePizza:) forControlEvents:UIControlEventTouchUpInside];
addTocartButton.backgroundColor = [UIColor colorWithRed:190.0f/255.0f green:19.0f/255.0f blue:31.0f/255.0f alpha:1.0];
// [cell.contentView addSubview:addTocartButton];
[cell.contentView insertSubview:addTocartButton atIndex:0];
addTocartButton.clipsToBounds = YES;
addTocartButton.layer.cornerRadius =10.0f;
}
}
cell.pizzaTopins.textColor = [UIColor colorWithRed:190.0f/255.0f green:19.0f/255.0f blue:31.0f/255.0f alpha:1.0];
cell.pizzaTopins.text = [dataArray objectAtIndex:indexPath.row];
cell.tileImage.image = [UIImage imageNamed:#"profile44.png"];
// cell.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
return cell;
}
dequeueReusableCellWithIdentifier will "reuse" previously created cells so you have to make sure to re-initialize all manually added/modified attributes including any buttons you added programatically.
Screen shots would help us properly diagnose the issue but your code is vulnerable to lingering attributes of reused cells so I would suggest starting with that.
BTW you would probably end up with simpler code if you defined multiple cell prototypes in IB and varied the cellIdentifier based on the indexPath row.

Issue assigning action to UIButton in TableView Cell creation

I am populating a TableView dynamically with data called from a server. I have 2 main files, one for the new cell and one for handling the population of the cells within the TableView Now when I assign [cell.button addTarget:self action:#selector(addFriend:) forControlEvents:UIControlEventTouchUpInside]; to my button within the cellForRowAtIndexPath and I put an output to log the action of the clicking of the button, nothing happens.
Here is what I am doing:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.......
/* Setup button action */
[cell.button addTarget:self action:#selector(addFriend:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)addFriend:(id)sender
{
NSLog(#"Add friend.");
}
Cell Creation:
HomePageTimelineCell *cell = (HomePageTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[HomePageTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Suggestions?
UPDATE:
Here is my cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%d",i);
self.tableView.separatorColor = [UIColor clearColor];// Get rid of speration border color
static NSString *CellIdentifier = #"homecell";
HomePageTimelineCell *cell = (HomePageTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[HomePageTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell initTimelineCell];
cell.backgroundColor = [UIColor clearColor];// Set the background color to the cell
//<!-- Set background color
if (indexPath.row % 2) {
cell.contentView.backgroundColor = [self colorWithHexString:#"77d1af"];
//<!-- Set background and text color for even cells
[cell.button setTitleColor:[self colorWithHexString:#"7ddcb8"] forState:UIControlStateNormal];
cell.button.backgroundColor = [self colorWithHexString:#"666666"];
} else {
cell.contentView.backgroundColor = [self colorWithHexString:#"7ddcb8"];
//<!-- Set background and text color for even cells
[cell.button setTitleColor:[self colorWithHexString:#"7ddcb8"] forState:UIControlStateNormal];
cell.button.backgroundColor = [self colorWithHexString:#"ffffff"];
}
//<!-- if the indexRow row = 0, this is the clients username and address
if(indexPath.row == 0)
{
NSArray *get = [[SSKeychain allAccounts] init];
NSString *username = [get[0] objectForKey:#"acct"];
//<!-- Get keyname of the address and than point to that keyname and get data
NSString *KeyName = [[self.dataGrabed_dictionary allKeys] objectAtIndex: indexPath.row + 4];
//<!-- create uppercase strings
NSString *upperCaseAddress = [[self.dataGrabed_dictionary objectForKey:KeyName] uppercaseString];
NSString *upperCaseUsername = [username uppercaseString];
//<!-- Set associated strings
cell.addressLabel.text = upperCaseAddress;
cell.usernameLabel.text = upperCaseUsername;
}
//<!-- if the indexRow row = 1, this is the 2 cell and will show the most upcoming lawn schedule
else if(indexPath.row == 1)
{ //<!-- create uppercase strings
NSString *upperCaseDay = [[self.dataGrabed_dictionary objectForKey:#"upcoming_day"] uppercaseString];
//<!-- create uppercase strings
NSString *upperCaseDate = [[self.dataGrabed_dictionary objectForKey:#"upcoming_date"] uppercaseString];
cell.contentView.backgroundColor = [self colorWithHexString:#"666666"];
cell.button.backgroundColor = [self colorWithHexString:#"7ddcb8"];
cell.button.tag = indexPath.row;
[cell.button setTitle:#"change" forState:UIControlStateNormal];
[cell.button setTitleColor:[self colorWithHexString:#"666666"] forState:UIControlStateNormal];
/* Setup button action */
[cell.button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.dayLabel.text = upperCaseDay;//<!-- Set the day
cell.dateLabel.text = upperCaseDate;//<!-- Set the Date
[cell.contentView addSubview:cell.button];
[cell.contentView addSubview:cell.nextLabel];
[cell.contentView addSubview:cell.dayLabel];
[cell.contentView addSubview:cell.dateLabel];
}
else //<!-- Normal cell population
{
//<!-- Re edit labels positioning
cell.dayLabel.frame = CGRectMake(9, 10, 200, 45);
cell.dateLabel.frame = CGRectMake(9, 35, 300, 45);
//<!-- Setup data called from server
NSDictionary *innerClientData =[self.dataGrabed_dictionary objectForKey:#"scheduled_times"][i];
NSString *innerClientDay =[self.dataGrabed_dictionary objectForKey:#"scheduled_times_day"][i];
NSString *innerClientDate =[self.dataGrabed_dictionary objectForKey:#"scheduled_times_date"][i];
//<!-- create uppercase strings
NSString *upperCaseDay = [innerClientDay uppercaseString];
//<!-- create uppercase strings
NSString *upperCaseDate = [ innerClientDate uppercaseString];
//<!-- Check to see if client paid
if([[innerClientData objectForKey:#"client_paid"] isEqual: #"0"])
{
NSString *amount = [innerClientData objectForKey:#"client_price"];
NSString *pay = #"pay $";
NSString * combined = [pay stringByAppendingString:amount];
[cell.button setTitle:combined forState:UIControlStateNormal];
}
else if([[innerClientData objectForKey:#"client_paid"] isEqual: #"1"])
{
[cell.button setTitle:#"PAID" forState:UIControlStateNormal];
}
//[cell.button setTitleColor:[self colorWithHexString:#"666666"] forState:UIControlStateNormal];
cell.button.tag = indexPath.row;
/* Setup button action */
[cell.button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.dayLabel.text = upperCaseDay;//<!-- Set the day
cell.dateLabel.text = upperCaseDate;//<!-- Set the Date
[cell.contentView addSubview:cell.button];
[cell.contentView addSubview:cell.dayLabel];
[cell.contentView addSubview:cell.dateLabel];
i++;
}
return cell;
}
Here is my HomePageTimelineCell cell creator:
#import "HomePageTimelineCell.h"
#import "HomePage.h"
#implementation HomePageTimelineCell
#synthesize cellContentView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
cellContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 300, 85)];
cellContentView.backgroundColor = [UIColor clearColor];
_usernameLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 10, 300, 45)];
_usernameLabel.font = [UIFont fontWithName:#"AppleSDGothicNeo-SemiBold" size:30.0];
_usernameLabel.textColor = [UIColor whiteColor];
_addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 40, 300, 45)];
_addressLabel.font = [UIFont fontWithName:#"AppleSDGothicNeo-Light" size:20.0];
_addressLabel.textColor = [UIColor whiteColor];
_addressLabel.numberOfLines = 1;
_addressLabel.lineBreakMode = NSLineBreakByTruncatingTail;
_nextLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 0, 100, 45)];
_nextLabel.font = [UIFont fontWithName:#"AppleSDGothicNeo-Thin" size:25.0];
_nextLabel.textColor = [UIColor whiteColor];
_nextLabel.text = #"NEXT";
_dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 25, 200, 45)];
_dayLabel.font = [UIFont fontWithName:#"AppleSDGothicNeo-SemiBold" size:35.0];
_dayLabel.textColor = [UIColor whiteColor];
_dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(9, 50, 300, 45)];
_dateLabel.font = [UIFont fontWithName:#"AppleSDGothicNeo-Thin" size:25.0];
_dateLabel.textColor = [UIColor whiteColor];
// IMPACT BUTTON STYLES
_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_button.layer.cornerRadius = 0.0f;
_button.frame = CGRectMake(220, 20, 80, 45);
/* ADD ALL THE CONTENT */
[self addSubview:cellContentView];
[cellContentView addSubview:_usernameLabel];
[cellContentView addSubview:_addressLabel];
}
return self;
}
- (void) initTimelineCell
{
cellContentView.layer.cornerRadius = 2;
cellContentView.layer.masksToBounds = NO;
configured = YES;
}
- (bool) configured
{
return configured;
}
#end
Premise that you should provide more code to have a clear idea of what is happening,
this is not the best approach.
At least I hope you are adding the target just if the cell is nil and you are init that and not more and more time :)
But anyway, the correct approach in functionality and design, is to implement the button action in the cell subclass, and return back by a delegate the cell where the button touched was.
I explained the various approaches here, the last one is this.
Enjoy clean code and good design ;)

UITableView cells don't show labels and buttons

I have a UITableView that has its delegate and dataSource properties set correctly, however its content isn't showing.
MY Code
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
BOOL notInputNotes;
UIColor *normalBlue = [[UIColor alloc]initWithRed:0.1 green:0.65 blue:1.0 alpha:0.8];
HomeWork *hw = [hwArray objectAtIndex:indexPath.row];
NSString *dateDueAsText = [dateFormat stringFromDate:hw.dateDue];
CGFloat xForDate;
if (dateDueAsText.length < 16 || dateDueAsText.length == 16) {
xForDate = 215;
}
else{
xForDate = 214;
}
hw.indexPath = indexPath;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
for (UIView *view in cell.subviews) {
[view removeFromSuperview];
}
if (!isEditing) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell setEditing:NO];
}
else{
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
[cell setEditing:YES];
}
UILabel *subject = [[UILabel alloc]initWithFrame:CGRectMake(70, 5, 150, 45)];
if(![hw.subject isEqualToString:#""])
{
subject.text = hw.subject;
notInputNotes = NO;
}
else{
subject.text = hw.notes;
subject.frame = CGRectMake(70, 20, 150, 45);
notInputNotes = YES;
}
[subject setFont:[UIFont systemFontOfSize:20]];
[subject setTextColor:normalBlue];
subject.adjustsFontSizeToFitWidth = YES;
subject.backgroundColor = [UIColor blueColor];//test
[cell addSubview:subject];
if(!notInputNotes){
UILabel *notes = [[UILabel alloc]initWithFrame:CGRectMake(70, 40, 300, 30)];
notes.text = hw.notes;
[notes setFont:[UIFont systemFontOfSize:12]];
notes.adjustsFontSizeToFitWidth = YES;
[notes setTextColor:[UIColor colorWithRed:0.2 green:0.3 blue:1.0 alpha:1.0]];
[cell addSubview:notes];
}
UILabel *importancy = [[UILabel alloc]initWithFrame:CGRectMake(300, 45, 200, 30)];
importancy.text = hw.importancy;
[importancy setFont:[UIFont systemFontOfSize:12]];
[importancy setTextColor:[UIColor redColor]];
importancy.adjustsFontSizeToFitWidth = YES;
[cell addSubview:importancy];
UILabel *dateDue = [[UILabel alloc]initWithFrame:CGRectMake(xForDate, 10, 200, 30)];
dateDue.text = [NSString stringWithFormat:#"%#",dateDueAsText];
[dateDue setFont:[UIFont systemFontOfSize:13]];
dateDue.textColor = normalBlue;
dateDue.adjustsFontSizeToFitWidth = YES;
[cell addSubview:dateDue];
hw.doneButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 25, 40, 40)];
if(!hw.done){
[hw.doneButton setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
else{
[hw.doneButton setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
[hw.doneButton addTarget:hw action:#selector(done:) forControlEvents:UIControlEventTouchUpInside];
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = -1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *nowMinus1 = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];
NSComparisonResult result;
result = [nowMinus1 compare:hw.dateDue];
if(result==NSOrderedDescending && !(hw.done)){
cell.backgroundColor = [UIColor colorWithWhite:0.86 alpha:0.6];
NSLog(#"%# is earlier than %#",dateDueAsText,[dateFormat stringFromDate:nowMinus1]);
[hw.doneButton setImage:[UIImage imageNamed:#"lighter-gray_unckecked.jpg"] forState:UIControlStateNormal];
hw.late = YES;
}
else{
cell.backgroundColor = [UIColor whiteColor];
hw.late = NO;
}
[cell addSubview:hw.doneButton];
return cell;
}
Does somebody know what's wrong there? Thanks in advance.
Note
the cell's background color appears different if you set it, so the cells are there and re correctly made the problem is really with the views
I wouldn't say it is a really good practice to remove all subviews of a UITableViewCell and add your own subviews after. It would be better to subclass UITableViewCell.
That being said, you can try this instead, it will display your content.
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
But your problem definitely comes from you wanting to remove the subviews. If the first problem is cell overlapping try implementing
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return customHeight;
}

UIButton is 0x0000 when reload TableView in ios

I added UIButton to each uitableview row but when i nedd reloadData, uibutton is 0x0000. Can you give me any suggestions? Thanks much.
I used this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
// NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{*/
NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)];
pricelabel.backgroundColor = [UIColor clearColor];
pricelabel.font = [UIFont fontWithName:#"Helvetica" size:16];
pricelabel.font = [UIFont boldSystemFontOfSize:16];
pricelabel.textColor = [UIColor darkGrayColor];
pricelabel.tag = 3000;
//pricelabel.hidden = YES;
pricelabel.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview: pricelabel];
[pricelabel release];
UIButton * market = [[UIButton alloc] init];;
[market setFrame:CGRectMake(200, 6, 30, 30)];
market.tag = 4000;
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
// [market setTag:indexPath.row];
[cell.contentView addSubview:market];
}
if([priceNewArray count]> 0)
{
UILabel *pricelbl = (UILabel*)[cell.contentView viewWithTag:3000];
pricelbl.text =[NSString stringWithFormat:#"$%#",[priceNewArray objectAtIndex:indexPath.row]];
if ([sellingArray count]>0) {
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"2"]){
pricelbl.hidden = NO;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]){
pricelbl.hidden = YES;
}
}
}
UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000];
[marketButton setTag:indexPath.row];
if([sellingArray count]>0)
{
NSLog(#"sellingArray %#",sellingArray);
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]) // nothing
{
[marketButton setSelected:NO];
[marketButton setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"2"]) // marketplace
{
[marketButton setSelected:YES];
[marketButton setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
}
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
I used above code, when reloadData, data of Price label is changed fine but marketbutton can not changed image. I debug after call [_tableview reloadData];, marketButton is 0x0000 .
I think your comparison is not correct. You are trying to compare integer with string.
If i asumed correct then try something like this.
int sellingPrice=0;
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString: [NSString stringWithFormat:#"%i",sellingPrice]]) // nothing
sellingPrice=2;
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:[NSString stringWithFormat:#"%i",sellingPrice]]) // marketplace
use single CellIdentifier for cell when using same designs for cell,it used to reuse tableview cells
NSString *CellIdentifier = #"Cell"
please remove this code [marketButton setTag:indexPath.row]; you not want to change tag for cell,you not set tag for identifier for UIButton, change this two thinks your code will work

Cannot access tableview cell UISearchBarController

I have a custom UITableView Cell. It contains a custom button with image that shows a check box which can be tapped to show a checked and unchecked image. It works correctly, but once filtering begins using UISearchBarController tapping my custom button within a cell the image does not update properly. Are the results shown in perhaps a different tableview? The methods to update the cell are all called, but the image does not change.
What is more perplexing is: Scrolling the cell out of view and back causes the cell to be displayed correctly.
#define MAINLABEL_TAG 1
#define SECONDLABEL_TAG 2
#define CHECKBOX_TAG 3
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UILabel *mainLabel, *secondLabel;
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
PDFFile *newPDF = [_pdfDocumentFiltered.pdfFileList objectAtIndex: indexPath.row];
//compute label sizes
GLfloat heightOfName = [TextSize computeHeightWithBoldFont: newPDF.documentTitle andViewWidth: 250 andFontSize: 18];
CGFloat heightOfDescription = [TextSize computeHeight: newPDF.description andViewWidth: 250 andFontSize: 16];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
//set main label attributes
mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0, 5.0, 250.0, heightOfName)];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont boldSystemFontOfSize: 18.0];
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.textColor = [UIColor blackColor];
mainLabel.autoresizingMask = UIViewAutoresizingNone;
mainLabel.numberOfLines = 9;
mainLabel.lineBreakMode = UILineBreakModeWordWrap;
[cell.contentView addSubview:mainLabel];
//set second label attributes
secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0, heightOfName + 10, 250.0, heightOfDescription)];
secondLabel.tag = SECONDLABEL_TAG;
secondLabel.font = [UIFont systemFontOfSize:16.0];
secondLabel.textAlignment = UITextAlignmentLeft;
secondLabel.textColor = [UIColor darkGrayColor];
secondLabel.autoresizingMask = UIViewAutoresizingNone;
secondLabel.numberOfLines = 20;
secondLabel.lineBreakMode = UILineBreakModeWordWrap;
[cell.contentView addSubview:secondLabel];
[button setFrame: CGRectMake(5.0, 0.0, 52.0, heightOfName + heightOfDescription + 15)];
[button setTag: CHECKBOX_TAG];
[button addTarget: self action: #selector(checkUncheck:) forControlEvents: UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
} else {
mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
[mainLabel setFrame: CGRectMake(60.0, 0.0, 250.0, heightOfName)];
secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
[secondLabel setFrame: CGRectMake(60.0, heightOfName + 10, 250.0, heightOfDescription)];
button = (UIButton *)[cell.contentView viewWithTag:CHECKBOX_TAG];
[button setFrame: CGRectMake(5.0, 0.0, 52.0, heightOfName + heightOfDescription + 15)];
}
mainLabel.text = newPDF.documentTitle;
secondLabel.text = newPDF.description;
if ([newPDF.check boolValue] == YES)
{
NSLog(#"Updating image to YES %i", indexPath.row);
[button setImage: [UIImage imageNamed: #"ButtonCheck"] forState: UIControlStateNormal];
}else{
NSLog(#"Updating Image to NO");
[button setImage: [UIImage imageNamed: #"ButtonUncheck"] forState: UIControlStateNormal];
}
cell.tag = indexPath.row;
return cell;
}
- (IBAction) checkUncheck:(id)sender
{
UIButton *sendingButton = (UIButton *) sender;
UITableViewCell *cell = (UITableViewCell *)[[sendingButton superview] superview];
PDFFile *newPDF = [_pdfDocumentFiltered.pdfFileList objectAtIndex: cell.tag];
[newPDF setCheck: [NSNumber numberWithBool: ![newPDF.check boolValue]]];
[self.table reloadData];
}
You should not tag the cell but you should tag the button correctly.
[button setTag: indexPath.row]; //Tag it properly
button = (UIButton *)[cell.contentView viewWithTag:indexPath.row]; //Fetch it properly
In IBAction,
- (IBAction) checkUncheck:(id)sender
{
UIButton *sendingButton = (UIButton *) sender;
PDFFile *newPDF = [_pdfDocumentFiltered.pdfFileList objectAtIndex: sendingButton.tag];
[newPDF setCheck: [NSNumber numberWithBool: ![newPDF.check boolValue]]];
[self.table reloadData];
}
Reloading the searchDisplayController is necessary. Not sure why but adding this line solves the problem:
[self.searchDisplayController.searchResultsTableView reloadData];
So the updated method is the following:
- (IBAction) checkUncheck:(id)sender
{
UIButton *sendingButton = (UIButton *) sender;
UITableViewCell *cell = (UITableViewCell *)[[sendingButton superview] superview];
PDFFile *newPDF = [_pdfDocumentFiltered.pdfFileList objectAtIndex: cell.tag];
[newPDF setCheck: [NSNumber numberWithBool: ![newPDF.check boolValue]]];
[self.table reloadData];
[self.searchDisplayController.searchResultsTableView reloadData];
}

Resources