IOS - Add Cells at particular index with uistepper - ios

I am displaying dynamic data in UITableView with checkbox button in it. When User Select Checkbox button it will show UIStepper just below it. I dont know how to achieve this, what I exactly required is mentioned in the url below.
Target To achieve
I have just displayed the data in UITableView yet as mentioned in the code below. I don't know how to achieve this whether should i add cells below every index when user select checkbox or there some other good approach that i should follow.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [getNutrients count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"NutrientCell";
UITableViewCell *nutrientCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
nutrientCell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"texture_bg.png"]];
UILabel *nutrientLabel = (UILabel*)[nutrientCell viewWithTag:1];
nutrientLabel.text = [[getNutrients objectAtIndex:indexPath.row]valueForKey:#"name"];
return nutrientCell;
}

UITableView has property to insert row or section.
You can use insertRowsAtIndexPaths:withRowAnimation:
deleteRowsAtIndexPaths:withRowAnimation:
For insert and delete row respectively.

Related

Cell text won't appear in iOS 7 TableView

I've tried so many things and I'm totally at a loss. My text isn't appearing in my cell's default text labels in TableView. The strange part is everything else does work. By that I mean, I can read and parse the data source (XML), I can create the right amount of rows (5), and I can even get the cells to contain the text data. I know this because when I use NSLog it shows this. I even double check by using didSelectRowAtIndexPath and NSLog(#"%#",cellText); and the right text is in the log! So the cell text label contains the text I want, but it's still not appearing on the screen. Instead, I just get 5 blank rows that are each selectable, and will display the right text in the NSLog when I select them but not on the screen.
I've tried the following fixes: adjusting backgroundColor and textColor (nope), trying different sample code I could find online (nope), using ViewControllers instead of TableViewControllers (nope), deleting the files and re-building them (nope), and even creating new projects with only this code to isolate the code (nope). Every time, it's still blank! I have no idea what to do.
Here's the code I'm using to grab and display the data in the tableview:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [app.listArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
theList = [app.listArray objectAtIndex:indexPath.row];
cell.textLabel.text = theList.name;
NSLog(#"%#",cell.textLabel.text);
NSLog(#"%#",theList.name);
return cell;
}
I assume that this is a new plain project that does not have anything else that could interfere. You checked with NSLog(#"%#",cell.textLabel.text) that the text is populated, you verified that the background and foreground colors are not the same. One remaining thing to check is transparency/Alpha. Make sure it is set to 1.
If this is on the simulator, try testing on a device. If you can't test on a device, try different simulator device to rule out the possibility that it is a simulator glitch.
Also, try hardcoding the label text to something like #"Abcdef" to make sure that the source of the culprit is not the data you are trying to display.
Try like this way. see how I wrote cellForRowAtIndexPath method.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[cell.titleLabel setText:[listArray objectAtIndex:indexPath.row]];
NSLog(#"%#",cell.textLabel.text);
NSLog(#"%#",theList.name);
return cell;
}
I had this same problem but the answer to my problem was the Connection Inspector on the right hand side for the cell. The Outlet needs to be set to accessoryView for the ContentView. Just drag it over to the cell. Don't feel bad I pulled my hair out too.
Im using Xcode7 Beta and Swift

UITableViewCell hide content when is not visible

I have gif animated pictures in some uitableview rows,
When there is a lot of gifs in tableview CPU usage is going to be very high,
So I want to hide these gifs when their row is not visible,
How can I do that ?
How can I achieve not visible cells row's indexPath ?
After that I can hide the gif like that :
UITableViewCell *celll = [ tableView cellForRowAtIndexPath:indexPath];
UIImageView *gif = (UIImageView*)[celll viewWithTag:30000];
gif.hidden = TRUE;
So I must get not visible cells indexPath's in a loop.
Generally, you don't need to do that as long as you properly use reusable cells.
Nevertheless, if you do want to do that, you can use tableView:didEndDisplayingCell:forRowAtIndexPath: method on UITableViewDelegate:
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *gif = (UIImageView*)[celll viewWithTag:30000];
gif.hidden = YES;
}
If you'll put the line:
NSLog(#"%#",indexPath);
as the first line of the method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You'll realise that it's done automatically. Only the visible cells, i.e the one that appears on the screen, are the one you present the data for.
Sorry for poor english.You had not posted code about how your cellForRowAtIndexPath implementation look like,i will suggest to make your cellForRowAtIndexPath like below.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"YOURIDENTIFIERSTRING";
UITableViewCell *cell = [tableView dequeReusableCellWithIdentifier: CellIdentifier forIndexPath:indexPath];
// Configure the cell here …
return cell;
}
This way only those many cells will be created which are visible on device and once row goes out of device view it will be reuse for new row. Please check apple doc for tableview.

Update single section of multi-section UITableView

I have a UITableViewController created in storyboard. It has two sections. The first section's rows contain controls laid-out in storyboard. I want to update the rows in the second section using values in an array.
I'm fairly new to iOS development. I understand how to use a UITableViewDataSource to update a table based on the array, but not how to restrict the updates to a specific section. Can anyone outline how to do this?
EDIT This seemed like a simple problem, so I thought I code would just obscure the question. Maybe I was wrong. Heres what I have:
My numberOfRowsInSection function returns 1 in the section number is 0, because the first section (the one I designed in storyboard) has a single row, otherwise it returns the number of elements in the backing data array:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return 1;
else
return [myData length];
}
My cellForRowAtIndexPath function creates a cell if the section number is 1. But I don't know what to do if the section number is zero. How do I avoid having to recreate the rows I laid-out in storyboard?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 1)
{
cell.textLabel.text = [myData objectAtindex:indexPath.row];
}
else
{
// What to do here?
}
}
Well If you only have few static controls in the first section why won't you put these controls in a table header view instead? Thus you'll only have one section to worry about :)
In your method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathadd this
Create 2 differents UITableViewCells and reference them like this
if (indexPath.section == 1) {
NSString *CellIdentifier = #"DynamicCell";
VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//You are drawing your second section so you can use your array as values
cell.property1...
cell.property2...
cell.property3...
return cell;
}else{//If you have only 2 sections then else represent your first section
//You are drawing your first section
NSString *CellIdentifier = #"StaticCell";
VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
You can change the row value in the delegate method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
To identify the section, just use:
indexPath.section
You can use reloadRowsAtIndexPaths: with an array of all the indexPaths that are in the wanted section, built with a loop and a NSMutableArray.
- (void)reloadSections:(NSIndexSet *)sections
withRowAnimation:(UITableViewRowAnimation)animation;
The parameter "section" is An index set identifying the sections to reload.

First cell always empty in table. Display only from the second cell - Objective c

I have a table view which display the contacts from array. I setup the table view delegates by follows.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [contactArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ContactCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [contactArray objectAtIndex:indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60.0;
}
But the first cell in the table view always empty. It starts display only from second cell. I thought it may be header view. So I removed the header using the following delegate methods.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0.0;
}
But still have the problem. I attached the screenshot about this issue.
Any help will be appreciated.
Your TableView is fine and it is working correctly, this is due to some other problem that is included in iOS 7, that automatically scroll insets. To solve this problem, go to your storyboard and select the viewcontroller in which your TableView is and select the ViewController and select the Properties of that ViewController, and uncheck this checkbox, which is read as Adjust ScrollView Insets. See this screen shot,
Your table is correct.Just your table was auto adjusted by the viewController.
You can write self.automaticallyAdjustsScrollViewInsets = NO;
Your Deduction is wrong your first cell isn't missing, but your tableview has started by 64 points down. So change your frame of your tableview or your tableview constraints accordingly.
Tip : Try setting a background colour when you have to debug things like this to clear your doubts.

Custom empty cells in UITabelView

i got ios application with UITableView
I customize cells in this table, but when number of objects lower than visible cells on screen, all other cells look like simple UITableViewCells.
It should look like this
How should i customize all cells?
UPD Code for dataSource:
pragma mark - Table view data source
- (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 [dataBase.showMaps count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ShowMapCell *cell = (ShowMapCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ShowMapCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
ShowMap *sM = [dataBase.showMaps objectAtIndex:indexPath.row];
//cell.textLabel.text = sM.name;
[cell initShowMapCell:sM index:indexPath.row];
return cell;
}
You could have just set background color of the tableview. However that solution wont work because you are using different backgrounds for each cell.
So according to me to achieve what you want you need to add some extra (blank) rows to the table and for that you need to return your record + extra cell (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method. And above that you need to set the background color of table view to one of the cell background because if you are using bouncing effect in the tableview you will find white color when you scroll the tableview up/down.
If you want your UITableView not to have 'empty cells', you will need to hide the cell separators and change the background colour of the tableview to match your colour scheme.
As katzenhut said in the comments, they aren't cells, they are placeholders for content. So if you hide the separators and add them in the xib file for your UITableViewCell ShowCell, then they will no longer be seen when there isn't a cell to place in the table.

Resources