How to set separator of one section of tableView to clear - objective-c

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;
}

Related

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!!

UITextView in a grouped UITableView's cell

Please, explain me: how to programmatically create an UITextView in UITableViewCell (UITableView has a grouped style). The text view size should be equal to size of the cell.
The UITextView has a gap between its text and its border (top left corner), so I need the correct coordinates to properly place the text view on a cell.
UPDATE: I've solved my question. See my self-answer below. Thanks!
It was simple:
textView.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);
Here you go:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Prototype Cell"];
UITextField *textfield;
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"Prototype Cell"];
textfield = [[UITextField alloc] init];
[cell.contentView addSubview:textfield];
textfield.tag = TEXT_FIELD_TAG; //Suppose you defined TEXT_FIELD_TAG someplace else
}
else
{
textfield = [self.view viewWithTag: TEXT_FIELD_TAG];
textfield.frame = cell.frame;
}
return cell;
}
Set "contentInset" of UITextView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *sCellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDetail
reuseIdentifier:sCellIdentifier];
}
UITextView *textView = [UITextView alloc]init];
textView.contentInset = UIEdgeInsetsMake(-8,-8,-8,-8); //Change according ur requirement
textView.frame = cell.frame;
[textView setUserInteractionEnabled:FALSE];
[textView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubView:textView];
return cell;
}

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

UITableView Custom View still here even after reload

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.

Graphics in UITableView cells disappearing

Got a strange problem where I'm putting some text into a cell (using cell.textLabel) and a small "tick" graphic to the right of the cell. When I select the cell, the tick is supposed to appear, or disappear if it's already there. What actually happens is the tick appears then fades out again almost instantly. It's all pretty standard code, so if anyone's got any idea what's going on I'd be pleased to hear!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} else {
while ([[cell.contentView subviews] count] > 0) {
UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0];
[labelToClear removeFromSuperview];
}
}
NSString *theName = [[contacts objectForKey:[contactsKeys objectAtIndex:section]] objectAtIndex:row];
cell.textLabel = theName;
if (section == 0) {
cell.textLabel.textAlignment = UITextAlignmentCenter;
} else {
cell.textLabel.textAlignment = UITextAlignmentLeft;
}
if ([selectedContacts containsObject:theName]) {
CGFloat cellRight = tableView.frame.size.width - 70;
UIImage *theTickImage = [UIImage imageNamed:#"Tick.png"];
UIImageView *theTickImageView = [[[UIImageView alloc] initWithImage:theTickImage] autorelease];
theTickImageView.frame = CGRectMake(cellRight, 10, theTickImage.size.width, theTickImage.size.height);
[cell.contentView addSubview:theTickImageView];
}
return cell;
}
Many thanks for any help!
Well, I've figured it out - the cell.textLabel was sitting on top of my graphic, so obscuring it. I just set the backgroundColor property of the textLabel to [UIColor clearColor] and all was well - maybe I could have made my graphic sit on top of the textLabel, I haven't tried that yet.