I am creating a simple sign up/login screen. I am using static cells as follows:
The cells are static. I want that when I click the sign up button I add 2 more cells to the tableview. Is that possible considering that I am using static cells and not dynamic?
UPDATE:
I am trying to set the height to 0.0f inside the heightForRowAtIndexPath but it is blowing up.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // this line
if(cell.tag == 2)
return 0.0f;
return cell.frame.size.height;
}
When you reload the table and it asks for the size you would add 2 more to it. Just based on the row index you would decide to use the static cells or whatever else you are inserting.
Your question is not very clear but it appears that you have 2 cells in the image. That means you're trying to go from 2 to 4 cells once your signup code has run. Simply put the 4 static cells in your storyboard UITableView and implement the following method and call [self reloadData] to trigger it:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (SIGNUP_CODE_HAS_RUN == YES)
{
// where SIGNUP_CODE_HAS_RUN is set to YES after you've run your signup code
return 4;
}
else
{
return 2;
}
}
Related
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.
I have a static table view set up in interface builder, but in some circumstances I don't want some cells to show. What's the best way to accomplish this with static cells? I'd really like to use static cells because a) it makes sense because there is no datasource, and b) I'm rendering my static views using IBDesignable/IBInspectable and would like to keep it that way.
You should be able to add the UITableViewDelegate method tableView:heightForRowAtIndexPath: and return 0 for any row that you don't want to show.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat rowHeight = 44.0;
if (indexPath.row == /* a row you want to hide */) {
rowHeight = 0.0;
}
return rowHeight;
}
I need a grouped UITableView similar to the one for Twitter accounts in Settings app:
That is, a sort of form or menu where some of the sections have a beforehand known set of static cells, and some other sections have to be dynamic and allow inserting additional rows the same way the "Add Account" does here. I'm managing the UITableView in a .xib file. For the static cells, I have separated .xib files that I can load within the cellForRowAtIndexPath: method in the view controller.
How should I handle this kind of table? I donĀ“t find any example code.
How the cellForRowAtIndexPath: method should look like? May I need to keep strong properties for the static cells? Would it be better to design each static cell directly within the same .xib file where the table view is, and to set outlets for them? (Though this does not allow to reuse my custom cells design...)
I need some guidelines for achieving this and correctly managing cells and memory. Thanks in advance
Dynamic prototype cells can behave like static ones if you just return the cell without adding any content in cellForRowAtIndexPath, so you can have both "static like" cells and dynamic ones (where the number of rows and the content are variable) by using dynamic prototypes.
In the example below, I started with a table view controller in IB (with a grouped table view), and changed the number of dynamic prototype cells to 3. I adjusted the size of the first cell to 80, and added a UIImageView and two labels. The middle cell is a Basic style cell, and the last one is another custom cell with a single centered label. I gave them each their own identifier. This is what it looks like in IB:
Then in code, I did this:
- (void)viewDidLoad {
[super viewDidLoad];
self.theData = #[#"One",#"Two",#"Three",#"Four",#"Five"];
[self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 1)
return self.theData.count;
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0)
return 80;
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:#"TitleCell" forIndexPath:indexPath];
}else if (indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:#"DataCell" forIndexPath:indexPath];
cell.textLabel.text = self.theData[indexPath.row];
}else if (indexPath.section == 2) {
cell = [tableView dequeueReusableCellWithIdentifier:#"ButtonCell" forIndexPath:indexPath];
}
return cell;
}
As you can see, for the "static like" cells, I just return the cell with the correct identifier, and I get exactly what I set up in IB. The result at runtime will look like your posted image with three sections.
Static is just a provision on top of dynamic layout. Basically static is a WYSIWYG.
If you are not resistant to experiment with values, I would recommend go dynamic. There are hundreds of examples available, such as this and this.
As you go further, you would see yourself diverging towards two options when customizing dynamic table views:
Subclass UITableViewCell (more effort , but good in the long run). Again, follow this.
Play with UITableViewCell properties inside cellForRowAtIndexPath: (less effort and quick result but may or may not be performance-friendly due to possible redrawing)
The possibilities are endless, such as here where cell background view is customized.
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
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