UITableView Custom View still here even after reload - objective-c

I made a very simply table view, with load more function.
I have added a custom view on the last cell with text "Load More"
After the user clicked the load more function, the rows increased successfully.
But the text "Load More" didn't disappear.
Please help.
Here is my code.
- (void)viewDidLoad {
[super viewDidLoad];
noRow = 10;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return noRow+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.row != noRow ) { // As long as we haven’t reached the +1 yet in the count, we populate the cell like normal
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSString *cellValue = [[NSNumber numberWithInt:[indexPath row]] stringValue];
cell.text = cellValue;
} // Ok, all done for filling the normal cells, next we probaply reach the +1 index, which doesn’t contain anything yet
else if(indexPath.row == noRow ) { // Here we check if we reached the end of the index, so the +1 row
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *loadMore;
loadMore =[[UILabel alloc]initWithFrame: CGRectMake(0,0,320,50)];
loadMore.textColor = [UIColor blackColor];
loadMore.highlightedTextColor = [UIColor darkGrayColor];
loadMore.backgroundColor = [UIColor clearColor];
loadMore.font=[UIFont fontWithName:#"Verdana" size:20];
loadMore.textAlignment=UITextAlignmentCenter;
loadMore.font=[UIFont boldSystemFontOfSize:20];
loadMore.text=#"Load More..";
[cell addSubview:loadMore];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == noRow){
NSLog(#"noRow Prev: %d", noRow);
noRow += 5;
NSLog(#"noRow After: %d", noRow);
[self.tableView reloadData];
}else{
NSLog(#"IndexPath.row: %d", indexPath.row);
}
}

You are reusing the and you are not removing the view that you have added for load more
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[[cell viewWithTag:121] removeFromSuperview];//remove this tag view
if (indexPath.row != noRow ) { // As long as we haven’t reached the +1 yet in the count, we populate the cell like normal
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSString *cellValue = [[NSNumber numberWithInt:[indexPath row]] stringValue];
cell.text = cellValue;
} // Ok, all done for filling the normal cells, next we probaply reach the +1 index, which doesn’t contain anything yet
else if(indexPath.row == noRow ) { // Here we check if we reached the end of the index, so the +1 row
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *loadMore;
loadMore =[[UILabel alloc]initWithFrame: CGRectMake(0,0,320,50)];
loadMore.textColor = [UIColor blackColor];
loadMore.highlightedTextColor = [UIColor darkGrayColor];
loadMore.backgroundColor = [UIColor clearColor];
loadMore.font=[UIFont fontWithName:#"Verdana" size:20];
loadMore.textAlignment=UITextAlignmentCenter;
loadMore.font=[UIFont boldSystemFontOfSize:20];
loadMore.text=#"Load More..";
loadMore.tag = 121;// just setting the tag
[cell addSubview:loadMore];
}
return cell;
}

you should add loadMore as a subview to cell.contentView not cell.
after that add this line in your cellForRow ..
for (UIView *view in [cell.contentView subviews])
{
[view removeFromSuperview];
}
Currently your load more is being reused and is present in subsequent cells if reused.

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.

How to set separator of one section of tableView to clear

I have four sections in my tableView, and I want only the last section to have a clear separator. I know how to do this for the whole tableView, but cannot find any solution for my question. The only solution that I have thought of is to create the cell as an image in Photoshop and set that image as the cell background. Does anyone have an idea of how to do this without having to use Photoshop?
Try this one
In View didLoad
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
And
-(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 *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 1)];
view.backgroundColor = [UIColor greenColor];
view.tag = 101;
[cell addSubview:view];
}
if (indexPath.section == 0)
{
[[cell viewWithTag:101] setHidden:YES];
}
else if(indexPath.section == 1)
{
[[cell viewWithTag:101] setHidden:NO];
}
return cell;
}

Hide first Cell of UITableView on iPad

I have a TableView with various entries. Sometimes i need to scroll down, sometimes there are only a few entries, so everything is visible.
I want some settings and filters in my First Cell, but it should not be visible, only when the user scrolls down.
My Trys:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
float rowHeight = [self tableView:self.tableView heightForRowAtIndexPath:indexPath];
self.tableView.contentOffset = CGPointMake(0, rowHeight);
}
and
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTopanimated:NO];
}
Both did not work.
Any advice on my problem?
Are you looking for something like this?
[self.tableView setContentInset:UIEdgeInsetsMake(t,l,b,r)];
this will shift your tableView whatever way you want, but you can definitely scroll back.
Try your logic in the TableView Delegate methods
- (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];
}
if (indexPath.row == 0) {
//Your filters here
}
else {
//display the cell
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Also if you dont wish the first cell to be displayed just write
return nil; //for condition of indexpath.row == 0
And also adjust the height for the row in
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 0 && isDisplayThisSection) {//first visible
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 25)] autorelease];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(17, 10, 300, 25)];
[titleLabel setFont:[UIFont boldSystemFontOfSize:17.0]];
titleLabel.text = #"Temp Test";
[titleLabel setBackgroundColor:[UIColor clearColor]];
[headerView addSubview:titleLabel];
[titleLabel release];
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
}
set value to 0
Hope this help!!
Try your logic in the TableView Delegate methods
- (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];
}
if (indexPath.row == 0) {
//Your filters here
}
else {
//display the cell
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Also if you dont wish the first cell to be displayed just write
return ; //for condition of indexpath.row == 0
And also adjust the height for the row in
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
set value to 0
And if you wish to implement like Pull_to_Refresh, you would better write your logic in
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
Hope this help!!

Why we are checking if (cell == nil) in UITableViewController?

I am trying to implement UITableView based Application.For that I select UITableViewStyle is Group.In my TableView their is 15 section each section having 1 row.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 15;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==12)
{
return 120;
}
else
{
return 60;
}
}
I want add a UITextView on Section 12
For that i did following code
- (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];
}
if ([indexPath section] == 12)
{
if([indexPath row]==0)
{
descriptionTextField=[[UITextView alloc] initWithFrame:CGRectMake(5, 8, 290, 106)];
descriptionTextField.font = [UIFont systemFontOfSize:15.0];
descriptionTextField.backgroundColor=[UIColor scrollViewTexturedBackgroundColor];
[descriptionTextField setDelegate:self];
[descriptionTextField setTag:2];
[descriptionTextField setText:#"Enter Location Description."];
descriptionTextField.keyboardType=UIKeyboardTypeDefault;
descriptionTextField.returnKeyType=UIReturnKeyNext;
descriptionTextField.textColor=[UIColor blackColor];
descriptionTextField.editable=YES;
descriptionTextField.autocapitalizationType=UITextAutocapitalizationTypeWords;
descriptionTextField.autocorrectionType=UITextAutocorrectionTypeDefault;
descriptionTextField.textAlignment=UITextAlignmentLeft;
UIToolbar* keboardToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32)];
UIBarButtonItem *extra=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *Done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(keyboardDoneButtonActin:)];
[Done setWidth:65.0f];
[keboardToolBar setItems:[[[NSArray alloc]initWithObjects:extra,Done, nil]autorelease] ];
[extra release];
[Done release];
[keboardToolBar setTintColor:[UIColor blackColor]];
[keboardToolBar setAlpha:.70];
[descriptionTextField setInputAccessoryView:keboardToolBar];
[descriptionTextField setTag:101];
[cell.contentView addSubview:descriptionTextField];
[descriptionTextField release];
}
}
return cell;
}
In the initil stage the table view like this
if i am scrolling tableview up and down , then the uitextView section changed and it will display multiple location.
I can't understand my fault, why this happend?
if am implementing the above code in side the if(cell==nil)
- (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];
if ([indexPath section] == 12)
{
if([indexPath row]==0)
{
**/* implemention all code here*/**
[cell.contentView addSubview:descriptionTextField];
[descriptionTextField release];
}
}
return cell;
}
UITextView not disply, i think it is not allocating.
so what is the difference between code implementing in
if (cell == nil)
{
inside
}
if (cell == nil)
{
}
out side
The test if (cell == nil) handles the case where there are no reusable cells to dequeue, in which case you must create a new cell. When you create the new cell, you are responsible for constructing its view hierarchy.
NSString *CellIdentifier = [NSString stringWithFormat:#"%i",indexPath.row];
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
This can be used instead of
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
this is the simple example to write inside the cell ==nill
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
cell= nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[[cell.contentView viewWithTag:100+indexPath.row] removeFromSuperview];
UIView *selectview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 30)];
[selectview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"strip_s12A-1_h.png"]]];
cell.selectedBackgroundView = selectview;
[selectview release];
UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(40, 0, 300, 30)];
//cellTitle.adjustsFontSizeToFitWidth=YES;
[cellTitle setBackgroundColor:[UIColor clearColor]];
[cellTitle setFont:[UIFont fontWithName:#"Arial Rounded MT Bold" size:17]];
[cellTitle setTextColor:[UIColor blackColor]];
cellTitle.tag = 100+indexPath.row;
cellTitle.text= [[[cellArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] valueForKey:#"Des2"];
[cell.contentView addSubview:cellTitle];
[cellTitle release];
}
return cell;
}
i think will be enough right

Changing specific cells in uitableview

I have a uitableview but I want to disable some cells and leave others enabled.
I used the following code to disable all cells except for the first in the cellForRowAtIndexPath:
if ([indexPath row] > 0 ) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor lightGrayColor];
}
But instead of disabling all cells and enabling the first, it disabled all cells and enables the last. Why does this happen? And what can I do to get it to work?
BTW Here's my code for all cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
sectionContents = [[self listOfItems] objectAtIndex:[indexPath section]];
contentsForThisRow = [sectionContents objectAtIndex:[indexPath row]];
if ([indexPath row] > 0 ) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor lightGrayColor];
}
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = contentsForThisRow;
return cell;
}
Replace your code of cellForRowAtIndexPath: with this one
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
sectionContents = [[self listOfItems] objectAtIndex:[indexPath section]];
contentsForThisRow = [sectionContents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = contentsForThisRow;
if ([indexPath row] > 0 ) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor lightGrayColor];
}
return cell;
}
You don't show where you declare cell, but what you are doing is setting the userinteractionEnabled before getting the actual cell!
You have to put this customization code at the end of the method.
Also, try to declare the cell variable in the method, it has not to be used elsewhere.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
sectionContents = [[self listOfItems] objectAtIndex:[indexPath section]];
contentsForThisRow = [sectionContents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if ([indexPath row] > 0 ) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor lightGrayColor];
}
cell.textLabel.text = contentsForThisRow;
return cell;
}