Altering selected state for UITableViewCell - objective-c

I've got a standard UITableView where each cell is a custom UIView. Everything works fine, except when the cell is selected, it turns blue (as expected) but the UIView is hidden!
How can I change the contents of the view to be visible when the cell is selected?
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 300, 40)];
[headerLabel setText:[[[todayCollection events]objectAtIndex:indexPath.row]name]];
[headerLabel setFont:[UIFont boldSystemFontOfSize:22]];
headerLabel.highlightedTextColor = [UIColor whiteColor];
UILabel *venueLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 300, 20)];
[venueLabel setText:[[[[todayCollection events]objectAtIndex:indexPath.row]venue]name]];
[venueLabel setFont:[UIFont italicSystemFontOfSize:16]];
LBVenueBadge *venueBadge = [[LBVenueBadge alloc] initWithGenre:[[[todayCollection events]objectAtIndex:indexPath.row]genre] frame:CGRectMake(85, 73, 100, 20)];
UIImageView *pinImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 75, 18, 18)];
[pinImage setImage:[UIImage imageNamed:#"193-location-arrow.png"]];
UILabel *distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 75, 100, 15)];
[distanceLabel setFont:[UIFont systemFontOfSize:12]];
NSString *distanceString = [[[[todayCollection events]objectAtIndex:indexPath.row]venue]distanceString];
if([[[[todayCollection events]objectAtIndex:indexPath.row]performances]count] == 1)
{
UILabel *performanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 75, 50, 15)];
[performanceLabel setText:[[[[[todayCollection events]objectAtIndex:indexPath.row]performances]objectAtIndex:0]time]];
[performanceLabel setBackgroundColor:[UIColor clearColor]];
[performanceLabel setFont:[UIFont systemFontOfSize:12]];
[containerView addSubview:performanceLabel];
}else{
UILabel *performanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 75, 50, 15)];
[performanceLabel setText:[NSString stringWithFormat:#"%i shows", [[[[todayCollection events]objectAtIndex:indexPath.row]performances]count]]];
[performanceLabel setBackgroundColor:[UIColor clearColor]];
[performanceLabel setFont:[UIFont systemFontOfSize:12]];
[containerView addSubview:performanceLabel];
}
[distanceLabel setText:distanceString];
[containerView addSubview:pinImage];
[distanceLabel setBackgroundColor:[UIColor clearColor]];
[containerView addSubview:distanceLabel];
[headerLabel setBackgroundColor:[UIColor clearColor]];
[containerView addSubview:headerLabel];
[venueLabel setBackgroundColor:[UIColor clearColor]];
[containerView addSubview:venueLabel];
[containerView addSubview:venueBadge];
if(indexPath.row % 2 == 0)
{
[containerView setBackgroundColor:[UIColor colorWithRed:0.239 green:0.239 blue:0.232 alpha:0.05]];
}else{
[containerView setBackgroundColor:[UIColor colorWithRed:0.239 green:0.239 blue:0.232 alpha:0.02]];
}
cell.backgroundView = containerView;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
Thanks,

As the contentView property is readonly, I can add a subview to it instead.
[cell.contentView addSubview:containerView];

You are adding views to the background rather than the contentView of the cell replace this line.
cell.backgroundView = containerView;
with
[cell.contentView addSubview:containerView];

Related

Weird tableViewCell issue on iOS 6

These "pipe" characters are appearing in some of my cells on my table view, but only on iOS 6. The first screenshot shows the issue on iOS 6, and the lower screenshot is iOS 4.3:
I appreciate any help offered.
Here is the code I'm using:
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d%d", indexPath.section, indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIView *clearColor = [[UIView alloc] init];
clearColor.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = clearColor;
}
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 243.5, 25)];
[label1 setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16.0]];
[label1 setTextColor:[UIColor blackColor]];
label1.text = [[self.tableDataSource objectAtIndex:indexPath.row] objectForKey:#"Name"];
[cell addSubview:label1];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(253.5, 10, 243.5, 25)];
[label2 setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16.0]];
[label2 setTextColor:[UIColor blackColor]];
label2.text = [[self.tableDataSource objectAtIndex:indexPath.row] objectForKey:#"Email"];
[cell addSubview:label2];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(507, 10, 243.5, 25)];
[label3 setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16.0]];
[label3 setTextColor:[UIColor blackColor]];
label3.text = [[self.tableDataSource objectAtIndex:indexPath.row] objectForKey:#"Phone"];
[cell addSubview:label3];
UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(760.5, 10, 243.5, 25)];
[label4 setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16.0]];
[label4 setTextColor:[UIColor blackColor]];
label4.text = [[self.tableDataSource objectAtIndex:indexPath.row] objectForKey:#"Business"];
[cell addSubview:label4];
return cell;
}
click for larger image
Any specific reason you're using UILabels? I checked the code for an iOS6 application I have and I used NSStrings.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *newTitle = #"my string";
cell.textLabel.text = newTitle;
}
Turns out I had to set each label's background color to clear:
label.backgroundColor = [UIColor clearColor];
set clear background colour for labels
label.backgroundColor = [UIColor clearColor];

big tableview scrolling lags

i have a performance issue in my tableview
my cellForRow looks like that:
if (tableView == allMonthTable) {
static NSString *cellIdentifier = #"cell";
AllMonthCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[AllMonthCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] ;
}
Month *mo = [[dataHandler allMonth]objectAtIndex:indexPath.section];
double budgetValue = mo.budget.doubleValue;
[cell.budgetLabel setText:[NSString stringWithFormat:#"%.2f",budgetValue ]];
double spentValue = mo.spent.doubleValue;
[cell.spentLabel setText:[NSString stringWithFormat:#"%.2f",spentValue ]];
if (spentValue > 0) {
[cell.spentLabel setText:[NSString stringWithFormat:#"+%.2f",spentValue]];
[cell.spentLabel setTextColor:[self greenColor]];
}
else if (spentValue < 0){
[cell.spentLabel setText:[NSString stringWithFormat:#"%.2f",spentValue]];
[cell.spentLabel setTextColor:[self redColor]];
}
double balanceValue = budgetValue+spentValue;
[cell.balanceLabel setText:[NSString stringWithFormat:#"%.2f",balanceValue ]];
if (balanceValue > 0) {
[cell.balanceLabel setText:[NSString stringWithFormat:#"+%.2f",balanceValue]];
[cell.balanceLabel setTextColor:[self greenColor]];
}
else{
[cell.balanceLabel setText:[NSString stringWithFormat:#"%.2f",balanceValue]];
[cell.balanceLabel setTextColor:[self redColor]];
}
double avgDayValue = mo.avgDay.doubleValue;
[cell.avgDayLabel setText:[NSString stringWithFormat:#"%.2f",avgDayValue ]];
if (avgDayValue > 0) {
[cell.avgDayLabel setText:[NSString stringWithFormat:#"+%.2f",avgDayValue]];
}
int numberOfExpValue = mo.expenses.intValue;
[cell.numberOfExpLabel setText:[NSString stringWithFormat:#"%d",numberOfExpValue ]];
return cell;
}
return nil;
}
and my AllMonthCell.m files looks like that:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIFont *font = [UIFont fontWithName:#"Cochin" size:14.0];
UIFont *fontBold = [UIFont fontWithName:#"Cochin-Bold" size:14.0];
self.frame = CGRectMake(0, 0, 312, 100);
UIView *top = [[UIView alloc]initWithFrame:CGRectMake(4, 0, 304, 1)];
[top setBackgroundColor:[UIColor lightGrayColor]];
UILabel *budgetStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 102, 16)];
[budgetStringLabel setTextAlignment:UITextAlignmentLeft];
[budgetStringLabel setFont:font];
[budgetStringLabel setTextColor:[UIColor lightGrayColor]];
[budgetStringLabel setText:#"Month Budget:"];
[budgetStringLabel setBackgroundColor:[self grayColor]];
UILabel *spentStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 102, 16)];
[spentStringLabel setTextAlignment:UITextAlignmentLeft];
[spentStringLabel setFont:font];
[spentStringLabel setTextColor:[UIColor lightGrayColor]];
[spentStringLabel setText:#"Spent Money:"];
[spentStringLabel setBackgroundColor:[self grayColor]];
UIView *divide1 = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 292, 1)];
[divide1 setBackgroundColor:[UIColor lightGrayColor]];
UILabel *balanceStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 55, 102, 16)];
[balanceStringLabel setTextAlignment:UITextAlignmentLeft];
[balanceStringLabel setFont:font];
[balanceStringLabel setTextColor:[UIColor lightGrayColor]];
[balanceStringLabel setText:#"Month Balance:"];
[balanceStringLabel setBackgroundColor:[self grayColor]];
UIView *divide2 = [[UIView alloc]initWithFrame:CGRectMake(10, 74, 292, 1)];
UIView *divide3 = [[UIView alloc]initWithFrame:CGRectMake(10, 76, 292, 1)];
[divide2 setBackgroundColor:[UIColor lightGrayColor]];
[divide3 setBackgroundColor:[UIColor lightGrayColor]];
UILabel *avgDayStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 94, 200, 16)];
[avgDayStringLabel setTextAlignment:UITextAlignmentLeft];
[avgDayStringLabel setFont:font];
[avgDayStringLabel setTextColor:[UIColor lightGrayColor]];
[avgDayStringLabel setText:#"Per Day:"];
[avgDayStringLabel setBackgroundColor:[self grayColor]];
UILabel *numberOfExpStringLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 114, 102, 16)];
[numberOfExpStringLabel setTextAlignment:UITextAlignmentLeft];
[numberOfExpStringLabel setFont:font];
[numberOfExpStringLabel setTextColor:[UIColor lightGrayColor]];
[numberOfExpStringLabel setText:#"Expenses Made:"];
[numberOfExpStringLabel setBackgroundColor:[self grayColor]];
[self addSubview:budgetStringLabel];
[self addSubview:top];
[self addSubview:spentStringLabel];
[self addSubview:divide1];
[self addSubview:balanceStringLabel];
[self addSubview:divide2];
[self addSubview:divide3];
[self addSubview:numberOfExpStringLabel];
[self addSubview:avgDayStringLabel];
self.budgetLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 102, 16)];
[self.budgetLabel setTextAlignment:UITextAlignmentRight];
[self.budgetLabel setFont:font];
[self.budgetLabel setBackgroundColor:[self grayColor]];
[self.budgetLabel setTextColor:[UIColor darkGrayColor]];
self.spentLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 30, 102, 16)];
[self.spentLabel setTextAlignment:UITextAlignmentRight];
[self.spentLabel setFont:font];
[self.spentLabel setBackgroundColor:[self grayColor]];
[self.spentLabel setTextColor:[UIColor lightGrayColor]];
self.balanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 55, 102, 16)];
[self.balanceLabel setTextAlignment:UITextAlignmentRight];
[self.balanceLabel setFont:fontBold];
[self.balanceLabel setBackgroundColor:[self grayColor]];
self.avgDayLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 94, 102, 16)];
[self.avgDayLabel setFont:font];
[self.avgDayLabel setBackgroundColor:[self grayColor]];
[self.avgDayLabel setTextColor:[UIColor lightGrayColor]];
[self.avgDayLabel setTextAlignment:UITextAlignmentRight];
self.numberOfExpLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 114, 102, 16)];
[self.numberOfExpLabel setTextAlignment:UITextAlignmentRight];
[self.numberOfExpLabel setFont:font];
[self.numberOfExpLabel setBackgroundColor:[self grayColor]];
[self.numberOfExpLabel setTextColor:[UIColor lightGrayColor]];
[self addSubview:self.budgetLabel];
[self addSubview:self.spentLabel];
[self addSubview:self.balanceLabel];
[self addSubview:self.numberOfExpLabel];
[self addSubview:self.avgDayLabel];
[self setBackgroundColor:[self grayColor]];
}
return self;
}
here is a screenshot of time profiling while scrolling:
so i create reusable cells of the type AllMonthCell, and assign the values to them, is there a way to make it faster? i have pretty serious lags while scrolling the table..
a hint would be great :)
You should reuse the cells
Let me derive you a pattern
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
UILabel *noExpense;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
//Just the view alone should be created here no assigning of data here
//let me add just one custom view for easy understanding
noExpense = [[UILabel alloc]initWithFrame:CGRectMake(8, 20, 296, 16)];
[noExpense setTextAlignment:UITextAlignmentCenter];
[noExpense setFont:fontName];
//[noExpense setText:#"No data available yet."]; Dont do this
[noExpense setTextColor:[UIColor lightGrayColor]];
[noExpense setBackgroundColor:tableView.backgroundColor];
[noExpense setTag:100]; //Must set tag here
[cell addSubview:noExpense];
}
else
{
noExpense = [cell viewWithTag:100];
//You have to initialize your custom views which you created already.
//Just the view alone should be assigned here no assigning of data here
}
//Load all the data into the views here
[noExpense setText:#"Somedata which you have to set"];
return cell;
}
To know more about tableview see the doc. Its the bible for understanding tableviews.
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7

iOS: How to access custom labels on the contentView of an UITableViewCell correctly?

I have a UITableView with each cell being broken into several sections with a different label in each section. The table gets populated by an NSArray of NSDictionaries which contain all the data that populates the cell's labels. This part of the UITableView works great.
The problem arises when I change some of the values in one of the NSDictionaries and then reload the table with the updated NSArray. Normally, when I call [myTableView reloadData]; nothing is updated even though (through debugging) I can see the updated data being processed. But if I change the standard: if (cell == nil) { to if (1) {, in the cellForRowAtIndexPath method, then it works beautifully. I understand why if(1) { works but I do not understand why I cannot reuse the cells and just change the label text.
Why does if (cell == nil) not work? Is it a huge resource drain to re-initialize each cell?
CODE:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
if (/*cell == nil*/1) {
// Initialize Custom Cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//// Background View
cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 622, 43)];
[cellBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"Images/Library/phase_table_cell.png"]]];
//// Name Label
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 18)];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[cellBackgroundView addSubview:nameLabel];
//// Percent Complete Label
percentCompleteLabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 10, 30, 18)];
[percentCompleteLabel setBackgroundColor:[UIColor clearColor]];
[percentCompleteLabel setTextAlignment:UITextAlignmentCenter];
[cellBackgroundView addSubview:percentCompleteLabel];
//// Overall Status Label
overallStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(352, 7, 63, 30)];
[overallStatusLabel setBackgroundColor:[UIColor clearColor]];
[overallStatusLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
[overallStatusLabel setLineBreakMode:UILineBreakModeWordWrap];
[overallStatusLabel setNumberOfLines:2];
[overallStatusLabel setTextAlignment:UITextAlignmentCenter];
[cellBackgroundView addSubview:overallStatusLabel];
//// Finish Date Label
finishDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(425, 10, 55, 18)];
[finishDateLabel setBackgroundColor:[UIColor clearColor]];
[finishDateLabel setTextAlignment:UITextAlignmentCenter];
[finishDateLabel setFont:[UIFont systemFontOfSize:12.0]];
[cellBackgroundView addSubview:finishDateLabel];
//// Overall Weight Label
overallWeightLabel = [[UILabel alloc] initWithFrame:CGRectMake(505, 10, 30, 18)];
[overallWeightLabel setBackgroundColor:[UIColor clearColor]];
[overallWeightLabel setTextAlignment:UITextAlignmentCenter];
[cellBackgroundView addSubview:overallWeightLabel];
//// Green Risk View
greenRiskView = [[UIView alloc] initWithFrame:CGRectMake(557, 4, 61, 10)];
[greenRiskView setBackgroundColor:[UIColor greenColor]];
[greenRiskView setHidden:YES];
[cellBackgroundView addSubview:greenRiskView];
//// Yellow Risk View
yellowRiskView = [[UIView alloc] initWithFrame:CGRectMake(557, 17, 61, 10)];
[yellowRiskView setBackgroundColor:[UIColor yellowColor]];
[yellowRiskView setHidden:YES];
[cellBackgroundView addSubview:yellowRiskView];
//// Red Risk View
redRiskView = [[UIView alloc] initWithFrame:CGRectMake(557, 30, 61, 10)];
[redRiskView setBackgroundColor:[UIColor redColor]];
[redRiskView setHidden:YES];
[cellBackgroundView addSubview:redRiskView];
[cell.contentView addSubview:cellBackgroundView];
}
// Get Current Dictionary
NSDictionary *dictForIndexPath = [self.phaseArray objectAtIndex:[indexPath row]];
// Set Elements
[nameLabel setText:[dictForIndexPath objectForKey:#"name"]];
[percentCompleteLabel setText:[dictForIndexPath objectForKey:#"percentComplete"]];
[overallStatusLabel setText:[dictForIndexPath objectForKey:#"overallStatus"]];
[overallWeightLabel setText:[[NSNumber numberWithInt:[[dictForIndexPath objectForKey:#"overallWeight"] intValue]] stringValue]];
//// Create Finish Date String
NSString *finishDateString = [NSString stringWithFormat:#"%#/%#/%#", [dictForIndexPath objectForKey:#"finishDay"], [dictForIndexPath objectForKey:#"finishMonth"], [dictForIndexPath objectForKey:#"finishYear"]];
[finishDateLabel setText:finishDateString];
//// Pick Risk View
NSString *riskColor = [dictForIndexPath objectForKey:#"riskColor"];
if ([riskColor isEqualToString:#"Green"]) {
[greenRiskView setHidden:NO];
[yellowRiskView setHidden:YES];
[redRiskView setHidden:YES];
} else if ([riskColor isEqualToString:#"Yellow"]) {
[greenRiskView setHidden:YES];
[yellowRiskView setHidden:NO];
[redRiskView setHidden:YES];
} else {
[greenRiskView setHidden:YES];
[yellowRiskView setHidden:YES];
[redRiskView setHidden:NO];
}
return cell;
}
Probably you are assigning the value within the if(cell==nil) block? Only the initialization should happen within that block. Move the rest out of it. (Post your complete cellForRow code, if you need more help)
//edit: now, after you posted your code i see your problem:
you are storing all your labels and views in member variables.. but of course that only happens, when the if(cell != nil) block is executed. After that, you always access the same single cell (that was assigned last). So you are updating at least one cell ;)
To fix your problem, work e.g. with tags to get your corresponding views back from the cell. I will show it for your backgroundView, but you have to do it for all of your views (INSTEAD of the member variables. Remove them.)
static NSString *cellIdentifier = #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
if (cell == nil) {
//// Background View
UIView* cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 622, 43)];
cellBackgroundView.tag = 1;
[cellBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"Images/Library/phase_table_cell.png"]]];
}
// get the backgroundview from the current cell
UIView* backgroundView = [cell.contentView viewWithTag: 1];
and so on..

how to redirect to a new screen on the button click in the table view in objective c

I have created a table view and added some labels and displaying it, now i need a button in each cell, where on click on the button, it has redirect to the new screen.
The following is the code which i am trying to redirect,
- (void)viewDidLoad
{
vendorsArray = [CommonUserDetails sharedUserDetails].mVendorDictionary;
NSLog(#"vendorsArray is %# \n count is %d",vendorsArray,[vendorsArray count]);
loTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 43, 320, 400) style:UITableViewStylePlain];
[loTableView setBackgroundColor:[UIColor whiteColor]];
loTableView.delegate = self;
loTableView.dataSource = self;
loTableView.separatorColor = [UIColor grayColor];
[self.view addSubview:loTableView];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"myCell"];
if (cell == nil)
{
cell = [self tableviewCellWithReuseIdentifier:#"myCell"];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[self configureCell:cell forIndexPath:indexPath];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#define L1_TAG 1
#define L2_TAG 2
#define L3_TAG 3
#define L4_TAG 4
- (UITableViewCell *)tableviewCellWithReuseIdentifier:(NSString *)identifier
{
UITableViewCell* cell = [[UITableViewCell alloc]initWithFrame:CGRectMake(0, 0, 320, 100)reuseIdentifier:identifier];
UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 290, 30)];
[l1 setBackgroundColor:[UIColor clearColor]];
[l1 setFont:[UIFont systemFontOfSize:14]];
// [l1 setTextColor:[UIColor blackColor]];
l1.textColor = [UIColor colorWithRed:153/(CGFloat)255 green:51/(CGFloat)255 blue:0 alpha:1.0];
[l1 setAdjustsFontSizeToFitWidth:YES];
[l1 setTextAlignment:UITextAlignmentRight];
l1.tag = L1_TAG;
[cell.contentView addSubview:l1];
UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 20 , 300, 30)];
[l2 setBackgroundColor:[UIColor clearColor]];
l2.font = [UIFont boldSystemFontOfSize:14];
l2.textColor = [UIColor blackColor];
//l2.textColor = [UIColor colorWithRed:102/(CGFloat)255 green:102/(CGFloat)255 blue:153/(CGFloat)255 alpha:1.0];
l2.tag = L2_TAG;
[l2 setTextAlignment:UITextAlignmentLeft];
[cell.contentView addSubview:l2];
UILabel* l3 = [[UILabel alloc] initWithFrame:CGRectMake(5, 40, 190, 30)];
[l3 setBackgroundColor:[UIColor clearColor]];
[l3 setFont:[UIFont systemFontOfSize:14]];
[l3 setTextColor:[UIColor grayColor]];
// l3.textColor = [UIColor colorWithRed:102/(CGFloat)255 green:102/(CGFloat)255 blue:0/(CGFloat)255 alpha:1.0];
[l3 setAdjustsFontSizeToFitWidth:YES];
l3.tag = L3_TAG;
[cell.contentView addSubview:l3];
UILabel* l4 = [[UILabel alloc] initWithFrame:CGRectMake(5, 60, 190, 30)];
[l4 setBackgroundColor:[UIColor clearColor]];
[l4 setFont:[UIFont systemFontOfSize:14]];
[l4 setTextColor:[UIColor grayColor]];
//l4.textColor = [UIColor colorWithRed:0/(CGFloat)255 green:51/(CGFloat)255 blue:0/(CGFloat)255 alpha:1.0];
[l4 setAdjustsFontSizeToFitWidth:YES];
l4.tag = L4_TAG;
[cell.contentView addSubview:l4];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(150, 60, 70, 30);
[button setTitle:#"Pay" forState:UIControlStateNormal];
[cell.contentView addSubview:button];
[button addTarget:self action:#selector(payButton)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
UILabel *myl1 = (UILabel*)[cell viewWithTag:L1_TAG];
UILabel *myl2 = (UILabel*)[cell viewWithTag:L2_TAG];
UILabel *myl3 = (UILabel*)[cell viewWithTag:L3_TAG];
UILabel *myl4 = (UILabel*)[cell viewWithTag:L4_TAG];
NSDictionary *dictionary = [vendorsArray objectAtIndex:indexPath.row];
myl1.text= [NSString stringWithFormat:#"INo:%#",[dictionary objectForKey:#"iNo"]];
myl2.text=[dictionary objectForKey:#"vendorName"];
myl3.text= [NSString stringWithFormat:#"Amount:%#",[dictionary objectForKey:#"amount"]];
myl4.text= [NSString stringWithFormat:#"DDate:%#",[dictionary objectForKey:#"dDate"]];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
NSLog(#"count is %d",[vendorsArray count]);
return [vendorsArray count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"Payables";
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
-(IBAction) payButton{
PayDescriptionController *mPayDescriptionController = [[PayDescriptionController alloc]initWithNibName:#"PayDescriptionController" bundle:nil];
mPayDescriptionController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController presentModalViewController:mPayDescriptionController animated:YES];
}
Where i am going wrong in the above code.
try this,
[self.navigationController pushViewController:mPayDescriptionController animated:YES];
see ,it is navigating to next view or not !! and if not then may be one of your object is null.

TableView Reusing the Same Cells

In my UITableview, I am receiving a strange error. I have an array of dictionaries named _cellTextArray, and that dictionary contains the keys "textLabel", "detailTextLabel" and "cornerLabel". However I created three UILabel's which are added to the contentView of the cell and their text is set to their corresponding name in the dictionary. My problem is that the textLabels are only retrieving the first 5 objects in the dictionary, and using them for every cell rather than retrieving the object at the index of the cell in the dictionary.
Please could you tell me what I am doing wrong. I have combed through the code a few times, NSLog'ing the dictionaryValues for each indexPath, and they are all correct.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CheckBox *btn = (CheckBox *)[_checkboxArray objectAtIndex:indexPath.row];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[cell.contentView addSubview:btn];
//I added this code:
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:12.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 3; // 0 means no max.
UIImageView* img = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"gradient7.png"]] autorelease];
[cell setBackgroundView:img];
UILabel *lab = [[[UILabel alloc] initWithFrame:CGRectMake(40, 18, cell.contentView.frame.size.width-15, 22)] autorelease];
[lab setText:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:#"textLabel"]];
[lab setBackgroundColor:[UIColor clearColor]];
[lab setTextColor:[UIColor whiteColor]];
[lab setAdjustsFontSizeToFitWidth:YES];
[lab setTextAlignment:UITextAlignmentLeft];
[cell.contentView addSubview:lab];
UILabel *dlabl = [[[UILabel alloc] initWithFrame:CGRectMake(5, 54, cell.contentView.frame.size.width- 1, 22)] autorelease];
[dlabl setText:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:#"detailTextLabel"]];
[dlabl setTextColor:[UIColor colorWithRed:1.0 green:0.80 blue:0.0 alpha:1.0]];
[dlabl setBackgroundColor:[UIColor clearColor]];
// [dlabl setAdjustsFontSizeToFitWidth:YES];
[dlabl setTextAlignment:UITextAlignmentLeft];
[dlabl setFont:[UIFont systemFontOfSize:[lab font].pointSize - 3]];
[cell.contentView addSubview:dlabl];
UILabel *cornerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.frame.size.width - 40, 19, 40, 20)] autorelease];
[cornerLabel setTextColor:[UIColor whiteColor]];
//[cornerLabel setFont:[UIFont systemFontOfSize:12]];
[cornerLabel setAdjustsFontSizeToFitWidth:YES];
[cornerLabel setBackgroundColor:[UIColor clearColor]];
[cornerLabel setTextAlignment:UITextAlignmentCenter];
[cell.contentView addSubview:cornerLabel];
[cornerLabel setAdjustsFontSizeToFitWidth:YES];
[cornerLabel setText:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:#"cornerLabel"]];
// Adds image to cell
// UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(100, 5, 140, 50)] autorelease];
// [imageView setImage:[UIImage imageNamed:[[_cellTextArray objectAtIndex:indexPath.row] objectForKey:#"image"]]];
// [cell.contentView addSubview:imageView];
}
// Configure the cell.
return cell;
}
Check Listing 5-3 on this page (iOS Table View Programming Guide) to see how it is done properly.
You are only setting the data when there are no reusable cells. Once they are there, the table view won't ask for a new cell. So your data isn't being set. You are plainly returning the reused cell. You should set all the static properties (which don't depend on the row data) within that cell and bring all assignments outside the if statement. In this case, they seem to be the setText: statements of the three labels.