adding/removing a cell from grouped section in UITableView - ios

I am trying to add/remove cells to my grouped table dynamically during runtime. This is my first app that I have ever made, and while I have tried many other people's suggestions, I am still not getting it to work. Here is my code that I am using:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UILabel *lblName = (UILabel *)[cell viewWithTag:100];
[lblName setText:[intervalArray objectAtIndex:[indexPath row]]];
return cell;

You will need to update the array (will need to be an NSMutableArray) that is being used as the datasource for the UITableView. Then you can
[self.myTableView reloadData];
To reload the tableview
This is a good guide for dealing with updating TableView rows:
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/ManageInsertDeleteRow/ManageInsertDeleteRow.html

Related

How to change Custom Cell's UILabel Text?

I have a table view with a custom protoype cell and the cell has 3 different labels on it. How do I get access to these labels? I need to change their texts. I've searched around everywhere and haven't found something that helps me in this case. I have the below code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"basicCell" forIndexPath:indexPath];
//change cell's 3 labels' text here
return cell;
}
How can I access the above cell's 3 different labels that it has?
You need to make a class that is a subclass of UITableViewCell, and set the custom cell's class to the class you made. Then you want to link the labels using IBOutlets in the cell to the subclass you made.
Finally, in -tableView:cellForRowAtIndexPath:, you should cast the cell to your new subclass so you get access to those IBOutlets. Assuming your outlets are named someLabelOutlet1, someLabelOutlet2, and someLabelOutlet3, do the following after you finished subclassing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SomeTableViewCellSubclass *cell = (SomeTableViewCellSubclass *)[tableView dequeueReusableCellWithIdentifier:#"basicCell" forIndexPath:indexPath];
cell.someLabelOutlet1.text = #"sometext"
cell.someLabelOutlet2.text = #"sometext"
cell.someLabelOutlet3.text = #"sometext"
return cell;
}
In cellForRowAtIndexPath, you can access these labels
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (!cell) {
[tableView registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellReuseIdentifier:#"CustomCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
// set common properties here
cell.lbl1.textColor=[UIColor whiteColor];
cell.lbl1.backgroundColor=[UIColor redColor];
cell.lbl1.font=[UIFont fontWithName:#"Verdana" size:16.0];
}
cell.lbl1.text=#"lbl1Txt";
cell.lbl2.text=#"lbl2Txt";
cell.lbl3.text=#"lbl3Txt";
return cell;
}

UITableViewCell Reusing Dobut

I have this little doubt about reusing UITableViewCell.
When we create UITableViewCell it kinda looks like following.
- (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];
[self configureCell:cell forIndexPath:indexPath];
}
}
- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {/**Cell Config Code Goes Here**/}
}
So in my case, every cell in UITableView is different. And if UITableView reuses the cell the cell content is completely different.
Is it good practice to just pass CellIdentifier as nil so every time new cell is created instead given the condition that all cells are different ?
Or should I just move [self configureCell:cell forIndexPath:indexPath]; out and handle it on by my own ?
Cell reusability has its sense if you are using cell contents(same subviews) multiple time. Like you have two lables in your tableViewcell for all rows in your tableView. If you have small number of different cells. like if you have three types of cells to use multiple times in your tableView, you can use cell reusability with 3 different cell identifier.
But if you have all different cell, then its fine if you skip cell reusability.
The proper way of using reusability of the tableView is shown below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"FollowerCell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[self configureCell:cell forIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {/**Cell Config Code Goes Here**/}
}
The basic idea of reusability is that every time the similar type of cell should not be created, instead they should be reused just by updating their content.
What happens behind the scene is that there is a queue created in which these similar cells are added. Now Suppose there are 200 rows with different data but only 10 rows are visible. That in the queue only approx 14 cells will be present. Now as you will scroll the tableview up or down, this condition
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
checks wether the queue contains any cell, if yes a cell is fetched from the queue. Also the cells, which were earlier visible now on disappearing are added into the queue. This way everytime instead of new cells are created, the already created cells are used.
Now if you forcely make the cell = nil, than every time new cells will be created and added in the queue. Now if there are 200 data than queue will be containing 200 cells thus resulting in increase in memory size.
Hope it will help you in understanding the tableView. Happy Coding :)
I am afraid you have to move the configure cell code out of if condition to make every cell has its own content.
- (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];
}
[self configureCell:cell forIndexPath:indexPath];
}
While we saying reuse of UITableViewCells, we mean that we don't have to create the UIView hierarchy inside the cell every time. But you need to configure the content for different cells. Like cell.titleLabel.text = xxxx.
Meanwhile, you can use multiple reuseIdentifiers for different kind of cells. Or if you only have one such cell, you can define a cell as an attribute instance so that you don't have to create it everytime.

Custom UItableViewCell label is overlapping

Using UITableView and found that cells are not dequeuing properly.I have created prototype cells using storyboard,and have UIlabel over cell.
- (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];
}
UILabel*textLabel = (UILabel*)[cell viewWithTag:11];
textLabel.font=[UIFont fontWithName:#"MyUnderwood" size:16];
textLabel.text = [_tableLabelArray objectAtIndex:indexPath.row];
return cell;
}
Here is the image of UITableView which i am getting when i scroll the TableView
This could simply be a minor mistake. Try using a different tag value, or verify the values being used in the prototype cell.
You're not using your custom cell in CellForRowAtIndexPath. Code looks fine just inseatd of UITableViewCell dequeue your custom cell like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell"; //needs to be the identifier you defined in story boards
YourCustomCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell=[[YourCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel*textLabel = (UILabel*)[cell viewWithTag:11];
textLabel.font=[UIFont fontWithName:#"MyUnderwood" size:16];
textLabel.text = [_tableLabelArray objectAtIndex:indexPath.row];
return cell;
}
Then you need to create a subclass of UITableViewCell and name it YourCustomCell and link all IBOutlet objects like labels etc. put this line of code on top of your .h file #import YourCustomCell.h and simply access all properties of that cell like cell.textLabel instead of createting label and casting it to view with tag.
I fixed this issue by using "nil" instead of cellIdentifier
eg:
Instead of
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Use this
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:nil];
It did the trick for me
You just need to tick the "clears graphic context" property of the label in storyboard.

UITableView disappearing content

As soon as the table view gets touched the cell titles (and on-tap actions) disappear. I only use standard table view cells and store the values in an array. After the values disappear the table stays scrollable. Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [[systeme objectAtIndex:indexPath.row] description];
cell.backgroundColor = [UIColor clearColor];
[cell.textLabel setTextColor:[UIColor whiteColor]];
[cell.textLabel setTextAlignment:NSTextAlignmentCenter];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:#"choseSystem" object:[systeme objectAtIndex:indexPath.row]];
}
You should be sure that the reuse identifier is the same for all cells if you use only one type of cells. You should do something similar to the following in the portion of your code where to retrieve a reusable cell:
NSString *CellIdentifier = [NSString stringWithFormat:#"CellReuseIdentifier", (long)indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
And make you you set the #"CellReuseIdentifier" in your xib file or your storyboard.
If you would like to use multiple custom cells for a table view you should do something similar to what you're doing, but take into account that reuse identifiers need to be configured for every type of cells.
Hope this helps!
The table view was fine. I just added its view as a subview to another view without keeping reference to the actual UITableViewController. That was the problem.

Decreasing images in TableViewController when scrolling

I'm trying to display a UITableView with a list of artists who read from Last.fm API. I store all the artists in an array, then I show a table with your name and your picture.
Initially the photos look good, but when I do scroll the images are very small.
This is the initial appearance:
This is the appearance after scrolling, with the problem:
This is my code for create the cells:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TopArtistCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
JSCArtist *artist = self.artists[indexPath.row];
cell.textLabel.text = [artist name];
[cell.imageView setImageWithURL:[NSURL URLWithString:[[artist photo] thumbnail]] placeholderImage: [UIImage imageNamed:#"default_photo.jpeg"]];
return cell;
}
The default imageView for a UITableView acts a little odd. I would just create a custom UITableViewCell subclass and do your own layout.

Resources