Adding background for TableView sections iOS - ios

I have a section with dynamic multiple rows and I need to separate all the section with the background as shown in the image below.
Specifically those border lines for all the section. Pease let me know how can I make it possible.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return ProductArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *arr = [ProductArray objectAtIndex:section];
return arr.count;
}
Please refer to this
.
I tried adding header and footer, But I am not understanding how to add that rectangle box image for the entire section.

Put View in your cell after make outlet of that view. And use to cellForRowAtIndexPath this method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TableViewCell" forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.viewBackGround.backgroundColor = [UIColor redColor];
return cell;
}

For above requirement, I would like to suggest you the below approach.
create only one section with multiple rows.
Each row of UITableView will have UITableViewCell
Inside UITableViewCell create content view for which we are gonna add the border
Each row of UITableViewCell will have another UITableView(inside content view) whose methods are handled by UITableViewCell cells
Change UITableView (inside) height based on number of rows.
provide height of UITableViewCell based on the number of rows + orderID Header.

Related

How to add Margins on UITableView to inset content

I have a table view and I want to include margins so that the table's content has some breathing room on the left and right, and also between cells.
I managed to do a table view like:
My Storyboard design is like:
What I did was I added a UITableView to the main view and gave a margin of 10. I added constraints as shown in figure. Changed the Seperator style to None.
Then I added two UITableViewCells.
For Holding the data with custom row height 70.0
Row with same background of parentView with custom row height of 10.0
And implemented the methods like below:
// Row count (Twice as needed, for showing the insect)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 15*2;
}
// Returns cell based on indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
// Decides whether content or inset
if (indexPath.row%2)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"ReuseInset"];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:#"ReuseMe"];
cell.textLabel.text = #"MMP";
}
return cell;
}
// Returns custom row height based on indexpath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ if (indexPath.row%2)
{
return 10.0;
}
return 70.0;
}

Drawing string on uiview over contentview of tableviewcell draws same content again and again

Because using three labels over UITableViewCell slowed down tableview scroll performance I tried drawing directly on UIView that I dragged over the prototype cell. While this significantly improved scroll performance, this got me into another problem.
Actually I am drawing the contents of a feed. After six or seven unique rows (for 20 records), rows are duplicate. They show the same content starting from top of tableview. However When I tap on those repeated cells the content changes to what it should have been.
After researching I found six or seven is the number of rows actually visible on the screen. So this should have been display update error but I am not sure should I do to fix this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"newsCell";
NewsCell *cell = (NewsCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (indexPath.row < feeds.count) {
dict = [feeds objectAtIndex:indexPath.row];
[cell setNewsHeading:[dict objectForKey:#"title"] pubDate:[dict objectForKey:#"pubDate"] newsExcerpt:[dict objectForKey:#"attributedDescription"]];
}
dict = nil;
return cell;
}
-(void)setNewsHeading:(NSString *)newsHeading pubDate:(NSString *)pubDate newsExcerpt:(NSAttributedString *)newsExcerpt
{
self.newsView.newsHeading = newsHeading;
self.newsView.pubDate = pubDate;
self.newsView.newsExcerpt = newsExcerpt;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [feeds count];
}
That sounds like it could be a caching issue - have you tried overriding the prepareForReuse method in your cell subclass to reset the cell contents back to the default values?

uitableview hiding 2 cells from scroll

made a table view, and it does not make sense at all but my table view is hiding some cells.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 25;
}
- (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:#"Cell"];
}
object= [self.results objectAtIndex:indexPath.row];
cell.textLabel.text=[object valueForKey:#"hLabel"];
return cell;
}
It does render 25 cells but I am only able to view up to 23. 2 cells are hidden below. Though if I scroll further I can see them but if I leave the scroll the view comes back, 23rd being the last cell. Hence user is not able to select either 24 or 25.
Even if I reduce the cell count, lets say to 23, still the tableview is hiding 2 cells and I am not able to access them as the scroll ends at 2 cells prior.
I think your tableview height is more than screen height.
Decrease the height of the tableview (make it fit to view bound) and it will display all the cell.
And also check that this autoresizing constraint is setted in in the zib/storyboard for tableview.
I faced a similar issue and i don't know how but when i unchecked Autolayout, the issue gets resolved. Try turning off Autolayout. i hope it helps you too.

Custom "Floating" UITableViewCells

I have been searching the web for an answer to this and I am sure it has an easy answer.
I am creating an UITableView in my app and I am wanting it to have "floating" table view cells and a menu at the top. Like this:
I am sure that these are custom UITableView Cells, but I am not sure how to create them like this and have them be dynamic in size based on the content and how to include a menu at the top that disappears/shows once the user scrolls down or up.
Any insight on this would be awesome!
This can be done fairly easily with a subclassed UITableViewCell in a grouped table view. The image below shows one I quickly made by dragging in various UI elements, and creating a custom class, which has nothing but IBOutlets in the .h file.
The label with the gibberish in it is tied to the gray view below and to the top of the cell, with no specific height set, so when the cell grows, it will grow. Here is the code I used to populate the table:
- (void)viewDidLoad {
[super viewDidLoad];
self.theData = #[#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight",#"Nine"];
[self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.theData.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *s = #"asdfhfl fl flfh sflhsalfjh fajlhf lf asldf fh asljfafh sjlfh ajf fljf fasjlfhjfhjfhjsf hsjfhsjfhajsfh the end";
CGSize size = [s sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(281, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
return size.height + 130;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RDCell *cell = [tableView dequeueReusableCellWithIdentifier:#"RDCell" forIndexPath:indexPath];
cell.lbl1.text = self.theData[indexPath.section];
cell.lbl2.text = #"asdfhfl fl flfh sflhsalfjh fajlhf lf asldf fh asljfafh sjlfh ajf fljf fasjlfhjfhjfhjsf hsjfhsjfhajsfhajlfjafh";
return cell;
}
Notice that I set the number of sections to the count of the array, so you get separate sections of 1 row each. The code in the heightForRowAtIndexPath is typical of the way you would calculate the cell height (except that you would normally use the index path and get a different string for each cell).
I think thats what you are looking for..
IBScrollViewFloatingHeader

UITableView with image change after scrolling

I'm trying to create a custom UITableView with different cell identifiers. In the first cell should display an image and below to follow the rest of the cells. However when after scrolling the displayed image disappears. I tried to solve by looking for the answers provided by others for similar problems but without success.
Here's the code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
EventoCell *cell;
static NSMutableString *CellIdentifier;
if(i==0){
CellIdentifier = #"imgCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
i++;
}
else{
CellIdentifier = #"CellaEvento";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.Nome.text=[[AShow objectAtIndex:indexPath.row]GetEvento];
cell.Localita.text=[[AShow objectAtIndex:indexPath.row]GetLocalita];
cell.Ora.text=[NSString stringWithFormat:#"%d:%d",[[AShow objectAtIndex:indexPath.row]GetOra],[[AShow objectAtIndex:indexPath.row]GetMinuti]];
[cell setValue:[[AShow objectAtIndex:indexPath.row]GetEvento] :[[AShow objectAtIndex:indexPath.row]GetGiorno] :[[AShow objectAtIndex:indexPath.row]GetMese] :[[AShow objectAtIndex:indexPath.row]GetAnno] :[[AShow objectAtIndex:indexPath.row]GetOra] :[[AShow objectAtIndex:indexPath.row]GetMinuti]];
}
return cell;
}
Do you want to display image in the first row? If so, I think you can change the line for judge whether it is the first row from
if ( i==0 )
to
if (indexPath.row == 0 && indexPath.section == 0)
I think the i must be a class member. The UITableView only creates limited number of UITableViewCell. Commonly, the number equals to to the number of displaying rows. For example, if the screen can only display 10 rows, UITableView creates 10 cell. After scrolling, It reuses the created cells by calling dequeueReusableCellWithIdentifier:forIndexPath, this makes the rows which are out of screen release their cells. When scroll back, each "new" entered item needs a cell. The UITableView will call tableView:cellForRowAtIndexPath for new cell. Therefore as your scenario, only the first time, the image row can be displayed, because after the first time, the i is non-zero even scroll back.
I believe "i" was used as an instance variable in your code. Instead you should rely to the indexPath variable passed as an argument of the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method
Your very first item in your table view is identified by:
indexPath.row == 0 and indexPath.section == 0
.row is a row index within a given section .section
Make sure you have correctly implemented those two delegate methods also to correctly identify respectively your number of rows within a section and your number of sections:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

Resources