UITableViewCell not loading label on first time properly - objective-c

My TableViewCell is not loading content in first time after I scrolling up the table it will shows the full view.I have five sections in the tableView only (section =1) got this problem.
I have attached my codes here.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[super tableView:tableView cellForRowAtIndexPath:indexPath];
return cell;
}
On load view
After Scroll up

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return 10;
}
else if (section == 1)
{
return 10;
}
else
{
return 0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
if(section == 0)
{
return 40;
}
else if(section == 1)
{
return 40;
}
else
{
return 0;
}
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *View=[[UIView alloc]initWithFrame:CGRectMake(0,0,768,40)];
View.backgroundColor=[UIColor colorWithRed:211.0/255.0 green:211.0/255.0 blue:211.0/255.0 alpha:1.0];
UILabel *lbl_title=[[UILabel alloc]initWithFrame:CGRectMake(10,0,700,40)];
lbl_title.backgroundColor=[UIColor clearColor];
lbl_title.font= [UIFont fontWithName:#"ArialMT" size:20];
lbl_title.textColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
if (section == 0)
{
lbl_title.text=#"Used";
}
else if (section == 1)
{
lbl_title.text=#"Expiring";
}
[View addSubview:lbl_title];
[lbl_title release];
return View;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"cell %d",indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.section == 0)
{
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(180,10, 360, 60);
lbl.backgroundColor=[UIColor clearColor];
lbl.textColor = [UIColor blackColor];
lbl.numberOfLines = 3;
lbl.font = [UIFont fontWithName:#"ArialMT" size:18];
lbl.text = #"";
[cell.contentView addSubview:lbl];
[lbl release];
}
else if (indexPath.section == 1)
{
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(180,10, 360, 60);
lbl.backgroundColor=[UIColor clearColor];
lbl.textColor = [UIColor blackColor];
lbl.numberOfLines = 3;
lbl.font = [UIFont fontWithName:#"ArialMT" size:18];;
lbl.text = #"";
[cell.contentView addSubview:lbl];
[lbl release];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = arrayValues[indexPath.row];
return cell;
}

Related

didSelectRowAtIndexPath won't work at all

I have this code here:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MMSideDrawerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
switch (indexPath.section) {
case MMDrawerSectionOne:
if(indexPath.row == 0){
[cell.textLabel setText:#"Menu Item 1"];
}
break;
case MMDrawerSectionTwo:
if(indexPath.row == 0){
[cell.textLabel setText:#"Menu Item 2"];
}
break;
case MMDrawerSectionThree:
if(indexPath.row == 0){
[cell.textLabel setText:#"Menu Item 3"];
}else{
[cell.textLabel setText:#"Menu Item 4"];
}
break;
}
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.contentView.alpha = 0.7;
// Code to add background when user taps
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:(55.0/255.0) green:(55/255.0) blue:(55/255.0) alpha:0.3];
bgColorView.layer.masksToBounds = YES;
cell.selectedBackgroundView = bgColorView;
NSLog(#"%#", indexPath);
return cell;
}
and this code here:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Testing");
}
Whenever I tap a row on the UITable didSelectRowAtIndexPath does nothing. Is there anything I am doing wrong?
Peter
Open InterfaceBuilder and select your UITableView. A few questions to ask yourself:
Is "Selection" set to "Single" or "Multiple"? Having it set to "No Selection" will not fire didSelectRowAtIndexPath:
Is "User Interaction Enabled" checked? If not, didSelectRowAtIndexPath: will not fire
You can change these properties programmatically as follows:
self.tableView.allowsMultipleSelection = NO;
self.tableView.allowsSelection = YES;
self.tableView.userInteractionEnabled = YES;
If that doesn't work, try the accepted answer for this SO post here.

Item at indexpath zero is not showing until the indexpath 0 is scrolled out of the screen

I have a tableview in each cell of a tableview by creating custom cell for the main tableview. The problem is i can see the tableview content for all the indexpath except for the first one. If i scroll the tableview to make the first cell out of screen and scroll the first cell back then the content in the first cell is showing.
Please find my code below
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [itemArray count];
}
- (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];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
}
for (int i=0; i<[itemArray count]; i++) {
////NSLog(#"row is %d",i);
if (indexPath.row==i) {
cell.textLabel.text=[itemArray objectAtIndex:i];
}
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
Can anyone tell me why it is happening. I want the content of first cell to be seen on the screen also.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [itemArray count];
}
- (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];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
}
// why are you using this code in for loop ??
/*
for (int i=0; i<[itemArray count]; i++) {
////NSLog(#"row is %d",i);
if (indexPath.row==i) {
cell.textLabel.text=[itemArray objectAtIndex:i];
}
}
*/
cell.textLabel.text=[itemArray objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
Please update the below method as below. It may help you. And Please be sure You are reloading tableview after you add objects in itemArray.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
// add a placeholder cell while waiting on table data
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 1;
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
cell.textLabel.text=[[[DataManager GetInstance]getCurrentTableContent] objectAtIndex:indexPath.row];
}
return cell;

UITableView if(cell == null) error

I have this problem with my code, I am trying to add textfields to my cells. And it worked! But when I tried again afterwards it didn't. I debugged a little with NSLog and came to the conclusion that cell wasn't = nil and therefore my code wasn't being run.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Called1");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.accessoryType = UITableViewCellAccessoryNone;
NSLog(#"Called?");
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if([indexPath row] == 0) {
playerTextField.placeholder = #"yoyo1";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyNext;
} else {
playerTextField.placeholder = #"yoyo2";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyDone;
playerTextField.secureTextEntry = YES;
}
playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
playerTextField.textAlignment = UITextAlignmentLeft;
playerTextField.tag = 0;
playerTextField.clearButtonMode = UITextFieldViewModeNever;
[playerTextField setEnabled: YES];
[cell.contentView addSubview:playerTextField];
}
return cell;
}
The code NSLog(#"Called?") doesn't get called, but NSLog(#"Called1") does. Why?
Thanks.
Just a little change:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Called1");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
cell.accessoryType = UITableViewCellAccessoryNone;
NSLog(#"Called?");
//And then the rest of your cell configuration code...
Update your code to this, the way you initialize table cells is not correct.
[tableView dequeueReusableCellWithIdentifier:#"Cell"]; if this line returns non-nil your code will not correctly initialize the table cell. The logic is to get a new table cell by either dequeuing or re-using after you allocated a suitable table cell decorate it the same way.
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Called1");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [self reuseTableViewCellWithIdentifier:#"Cell" withIndexPath:indexPath];
}
cell.accessoryType = UITableViewCellAccessoryNone;
NSLog(#"Called?");
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if([indexPath row] == 0) {
playerTextField.placeholder = #"yoyo1";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyNext;
} else {
playerTextField.placeholder = #"yoyo2";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyDone;
playerTextField.secureTextEntry = YES;
}
playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
playerTextField.textAlignment = UITextAlignmentLeft;
playerTextField.tag = 0;
playerTextField.clearButtonMode = UITextFieldViewModeNever;
[playerTextField setEnabled: YES];
[cell.contentView addSubview:playerTextField];
return cell;
}

how to customize the tableview has shown in figure

How to create the tableview has shown below picture
.
If i created section i am missing th boarder line what to do ?
You need to set the UITableView to Grouped Style. Then you need 2 Sections, with no Header.
also you have to set the
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Example Code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if(section == 0)
return 1;
else
return 2;
}
- (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] autorelease];
}
// Configure the cell...
switch(indexPath.section)
{
case 0:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Create a Holiday Card";
break;
case 1:
switch(indexPath.row)
{
case 0:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Login to Photo";
break;
case 1:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = #"Create a Free Account";
break;
}
}
return cell;
}
Per example above
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;// Amount of sections
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0){
return 1;
}
else if (section == 1){
return 2;
}
}
- (void)tableViewUITableView *)aTableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
I assume you are referring to the STYLE of that example.
[self.view setBackgroundColor: [UIColor blueColor]];
/* also assuming your table sits on a UIViewController */
[myTableView setBackgroundColor: [UIColor clearColor]];
[myTableView setOpaque: NO];

How to change the row height for only the last cell in a tableview?

In my table view I had more than 200 rows and Im managing then by keeping loadmoreresults in last cell to display only 40 rows per page.when the user clicks on the load more results 40 more cells is displayed.I changed the row height of loadmoreresults cell as different from other. The problem is when there is no more results the height for other cell is effected with the height of the loadmoreresults cell I dont want to sffect the last row height to other rows.
The code I written as:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
ZohoAppDelegate* appDelegate = (ZohoAppDelegate*) [ [UIApplication sharedApplication] delegate];
int maxResults = [appDelegate.invoiceMaxValues intValue];
int noOfResults = [invoiceArray count] ;
if (noOfResults != 0 )
{
if (maxResults > noOfResults)
{
noOfResults++;
rowCount = noOfResults;
}
}
return noOfResults;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == (rowCount -1))
{
return 50;
}
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MasterViewIdentifier"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
UIView* elementView = [[UIView alloc] initWithFrame:CGRectMake(5,5,312,480)];
elementView.tag = 0;
[cell.contentView addSubview:elementView];
[elementView release];
}
UIView* elementView = [cell.contentView viewWithTag:0];
for(UIView* subView in elementView.subviews)
{
[subView removeFromSuperview];
}
UIImageView* imaeView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 78)];
if(indexPath.row == (rowCount-1))
{
elementView.backgroundColor = [UIColor colorWithRed:0.92578125 green:0.92578125 blue:0.92578125 alpha:1];
}
else
{
imaeView.image=[UIImage imageNamed:#"estimatecellbg.png" ];
}
[elementView addSubview:imaeView];
[imaeView release];
return cell;
}
Anyone's help will be much appreciated.
Thank you,
Monish.
Something along the lines of
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == (rowCount -1) && moreResults)
{
return 50;
}
return 80;
}