in method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
usually you use an array to set
[cell.textLabel setTex:#"row"];
but if I want to jump a row?
at example at indexpath.row I don't want to have this cell in my tableview, is possible?
Try this: Incorporate the tableView datasource method for row height.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == NUMBER_TO_AVOID) {
return 0.0f;
}
return 44.0f; //standard cell height
}
I have done this in several similar situations. What you'll do is create a second array "activeItems". Or something like that. Iterate through your main data array and build the active array with the valid items. Than have your data source reference this array instead. This gives you an array that is accurately indexed to your table.
You can put a condition in cellForRowAtIndexPath. Try this:
data = [array objectAtIndex:indexPath.row];
If (data != nil){
[cell.textLabel setText:data];
}
else{
// code that handles data if null
}
Related
I have a populated array which I can display in the tableview, but I want to hide 3 of the cells text (out of 7 cells). I know the below code is wrong, but in this case I only want to show the text in cell 0.
cell.animal.text[0] = animalarray[0]
cell.animal.hidden = true
Because you don't have codes, I can only use words to describe how it should be done.
You need to have an array of the unwanted text that you do not want to show.
Inside your cellForRowAtIndexPath, you need to have a for loop, to go through the animalarray, and within the for loop, have an if-else statement to check whether if(unwantedtext == animalarray), then cell!.textLabel.text = " "
You need to show me codes for me to help you.
I'm gonna try to help you in Objective C, hopefully I can make the logic so clear the language difference doesn't matter.
Generally you are telling the TableView what to print for each cell in the below delegate method in your ViewController.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.textLable.text = animalArray[indexPath.row];
return cell;
}
This is where you will decided which index in the animalArray you do or do not want to print. If your requirement is a static the simplest is to hardcode the the blocking.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(!(indexPath.row == self.indexIDontWantToPrint)) {
cell.textLable.text = animalArray[indexPath.row];
}
return cell;
}
If the indexes you do not want to print is dynamic and submitted to you by say an array.
You need to replace if(!(indexPath.row == self.indexIDontWantToPrint)) with checking if indexPath.row is inside the array of indexes you are to ignore.
NSArray has a handy containsObject method you can use to check if the array contains the current index the tableView wants to print. Be careful of the type difference of indexPath.row is NSInteger while NSArray needs to carry NSNumber for simple numeric numbers.
Adding more efficient logic than jo3birdtalk
1) Instead of having a extra array, you can creat an Object which contains a string & Bool.Add these objects in animalarray
2) Get the object from array at indexpath & check
if(animal.isShow == YES),if Yes show the text else hide label Or set blank string whatever you required
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
Animal * animal = animalArray[indexPath.row];
if(animal.isShow == YES)
{
show the text
}else
{
hide label Or set blank string whatever you requered
}
return cell;
}
I have a tableViewController, and under that i want one static cell and rest of all will be dynamic cells . I have already run for dynamic cells , but within same tableViewController i also need to add one static cell, how can i achieve it?
Please Help
Thanks in advance.
you could do something like the following:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dynamicContent.count + 1; // +1 for the static cell at the beginning
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
// static cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"StaticCellIdentifier" forIndexPath:indexPath];
// customization
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"DynamicCellIdentifier" forIndexPath:indexPath];
id contentObject = self.dynamicContent[indexPath.row];
// customization
return cell;
}
You cannot create static and dynamic cell at same time in a UITableViewController.
But you can hard code your static cell's data and load the data each time you reload your tableview.
You can keep all your cells in one section and keep checking for index path.row == 0 or create separate sections for them.
typedef NS_ENUM(NSUInteger, TableViewSectionType) {
TableViewSectionType_Static,
TableViewSectionType_Dynamic
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2; // One for static cell, and another for dynamic cells
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
switch(section) {
case TableViewSectionType_Static:
return 1; // Always return '1' to show the static cell at all times.
case TableViewSectionType_Dynamic:
return [myDynamicData count];
}
}
With this approach your cells will be split into two sections and it will be easier to manage. And it will always show one cell, as number of rows returned for TableViewSectionType_Static is 1 always. It will show the dynamic cells based on your data count.
The function below starts the creation of cells from zero to n, with content retrieved from the model. I have a few cells in my table at the beginning which has static contents and I want function below ignore them and start from nth place to insert new data. How will I do that? In other words, how can I return an already-existing cell at nth place?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
The method tableView:cellForRowAtIndexPath is needed for scrolling, as the tableView doesn't load all cells at once.
If you want to reload only a portion of your tableView you can use the method reloadRowsAtIndexPaths:withRowAnimation: or reloadSections:withRowAnimation:. This will reload only the specified cells.
If you have a performance problems, and don't want to load the static cells every time the tableView scrolls to them, you can store them in a property after they are created, and only return them. It would look somewhat like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row) {
case 0:
//Static cell
if (self.staticCell == nil) {
self.staticCell = [UITableViewCell new]; //Instantiate properly here
//Customize cell
}
return self.staticCell;
break;
case 1:
// Other cells normally
break;
}
}
In pseudo-code:
if (indexPath.row == 0)
return some static cell;
else if (indexPath.row == 1)
return some other static cell;
else
configure cell N with data[N-2];
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.
For example, I have 3 articles and when I display articles I want to display one more cell before the first one (that would be then 4 total).
I need to display that first article which is not in array and then articles which are in array.
UPDATE
I have tried next:
- (NSInteger)tableView:(
UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return ([arr count] + 1);
}
But my app then crash sometimes and I see over NSLOG then that app enters cellForRowAtIndexPath before I call [tableView reloadData].
This is something you should not really do.
Well, you can cheat the framework by returning a view (from -tableView:cellForRowAtIndexPath:) which would contain 2 subviews: your "first article" cell and the original first cell; don't forget to modify -tableView:heightForCellAtIndexPath: as well (otherwise your view would get cut).
But in general, you should change you data model behind the table view to dispay 4 cells – this is just a more valid approach.
You can do like this :
Return an additional row using this method :
// Each row array object contains the members for that section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [YouArray count]+1;
}
At the end check for this added row :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create a cell if one is not already available
UITableViewCell *cell = [self.mContactsTable dequeueReusableCellWithIdentifier:#"any-cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"any-cell"] autorelease];
}
//Identify the added row
if(indexpath.row==0)
{
NSLog(#"This is first row");
}
else{
// Write your existing code
}
}
You need to add a extra value in the array using insertObject
[arr insertObject:[NSNull null] atIndex:0];
And implement the methods like:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arr 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];
}
if([arr onjectAtIndex:indexPath.row] == [NSNull null])
{
// 1st cell extra cell do your stuff
}
return cell;
}
Do you keep all your articles in an array? You should add the new article to the array too. The array is your datasource. I take it that you'd want to insert the new article as the first element in your array if you'd like it to appear in the top cell. As soon as you've updated your array you should call [mytableView reloadData] which triggers all the datasource methods to be called and reload your table's data.