I am new to Xcode so please be a with me. I have been able to populate my tableview using a Custom Cell, the custom cell has 3 labels on it.
I need to access the value of one of the labels (myDateLabel) when the row is selected.
For a 'standard' cell I know I could use something like:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *theValue = cell.textLabel.text;
but I just can't figure out how to do this when using a custom cell, any help would be appreciated, I'm sure I'm missing something simple
Change you code from:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *theValue = cell.textLabel.text;
To:
CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.myDateLabel.text = #"text";
There's a great beginner tutorial here: http://www.appcoda.com/customize-table-view-cells-for-uitableview/
You can set tag property of your labels in storyboard (if you're using it) or in code when creating labels.
label1.tag = 1001;
label2.tag = 1002;
label3.tag = 1003;
Then you can enumerate through cell's subViews and check the tag of subview
for (UIView *subview in customCell.subviews)
switch (subview.tag)
{
case 1001:
NSString *s = [(UILabel)subview text];
break;
}
Related
I add a UITableView in storyboard and then add a a cell to that with my customisations. The cells were coming back blank and after debugging I found that the Cell being created isn't my custom one (I gave the cell a tag of 10 to test this).
My code is below:
UITableViewCell *cell = nil;
static NSString *cellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier ];
}
Food *curFood = [self.mealFoods objectAtIndex:indexPath.row];
/// Get the labels of cell
UILabel *descLabel = (UILabel *)[cell.contentView viewWithTag:101];
UILabel *quantityLabel = (UILabel *)[cell.contentView viewWithTag:102];
descLabel.text = curFood.Description;
quantityLabel.text = [NSString stringWithFormat:#"%f", curFood.Quantity];
return cell;
In my last project in ios6 the above worked fine, with the exception that the labels were not in contentview. Either I did something last time that I forgot or now in ios7 I need to do something to make it use my prototype/custom cell?
Micantox was correct in his comment. I had missed out setting the Cell identifier in the custom cell in storyboard:
In my case my identifier was simply 'Cell'.
I'm doing a project in which I have a TableView in which I have different Rows/Cell and in each cell there is a add button. What I need to do is on clicking the add button the new row should add below the row in which I clicked the button, means I have to add a cell to the next of the selected button on the row.and also a user can enter a text on the new row. Please tell me how to do this . As I'm new in iPhone development. I will be very thankful ....
Here is a click button function in which I want to perform an action to add new cell.
- (IBAction)AddButtonAction:(id)sender
{
[_choices insertObject:#"avav" atIndex:[_choices count]];
[self.tableView reloadData];
NSLog(#"here");
}
Use this delegate method to add a new row below the selected row. And use custom cell to have a text field on it...
rowCount ++;//Here rowcount refers the no. of rows in the table.
selectedIndexPath = indexPath;//Assign the selected indexpath for creating custom cell on it.
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];//Indexpath refers the currently selected/targeted cell.
In cellforRowAtIndexPath use like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if((selectedIndexPath) && (indexPath.row == selectedIndexPath.row) && (indexPath.section == selectedIndexPath.section))
{
static NSString *cellIdentifier=#"cell";
NewCell *cell = (NewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSString *customeCellName = [NSString stringWithFormat:#"%#",[NewCell class]];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (NewCell *) currentObject;
break;
}
}
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
else
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%d",indexPath.row];
// Configure the cell...
return cell;
}
}
OutPut will be like this
need to customize as per your requirement.
I consider you have textfeild in all cells. In AddButtonAction function save index of row. While creating cells make selected cell textfeild firstresponder. Once user enter data save it in array and again reload. Just keep track of selectedIndex properly.
Ifeel this will help.
If you add button to cell by addSubview method, simply you can get indexPath with the following code:
- (IBAction)AddButtonAction:(id)sender
{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)[button superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// Adding methods comes here...
}
If you are using some addSubview levels, you should use same superview methods inverse to access the UITableViewCell level. If using custom cells, replace your custom cell classname with UITableViewCell in the above code.
For adding cells, take a look at WWDC 2011 Videos, "UITableView Changes, Tips & Tricks" video. It shows some nice and useful methods to insert, delete and reload new rows/sections between other rows/sections like insertRowsAtIndexPaths:withRowAnimation: method. Or see Apple Documents.
In this screen shot you can see that I have added UITableView in UIViewController then customized the UITableViewCell by adding some labels in it. But the issue is when I run the application. All of the cells are empty. There are no labels at all.
I am not getting what can be the issue. I have searched the web and read tutorials but couldn't resolve it.
I resolved this issue by myself, just after little effort.
Actually when you create a custom cell you have to do these things:
Setup the labels, images etc on storyboard cell.
Create a custom cell class (inheriting from UITableViewCell)(CustomCell.h & .m), CustomCell.h having all of the properties as iboutlet for labels, images etc. and synthesize them all in implementation.
After creating this custom class go back to storyboard, select the custom cell, change its class to the CustomCell and give it some identifier like "MyCustomCell", then right click on the custom cell and connect the IBOutlets with labels etc.
Now import CustomCell class in the class where you are implementing the UITableView and use the properties you defined in CustomCell class.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyCustomCell";
CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the provided setImageWithURL: method to load the web image
// Ensure you use a placeholder image otherwise cells will be initialized with no image
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://example.com/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder"]];
cell.myCustomLabel.text = #"My Text";
return cell;
}
I did this all and my issue was resolved and please don't forget to connect the delegates & datasource and table.
Hope this will help others.
It is little bit late but you can solve your problem by setting the background color of your view as Clear Color from your storyboard.
in the tableview's delegate method , cellforrowatindexpath has a uitableviewcell inside it , there should be an identifier in the initialization of this cell. possibly it is "cell" or "cellIdentifier" .
you just need to select your cell from storyboard and enter this identifier string to the storyboard , where uitableviewcell's attribute inspector stays.
hope this helps.. :)
First Set cell identifier in storyboard #"Cell"
then set tag of label
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
UILabel *lblDate = (UILabel*)[cell viewWithTag:101];
UILabel *lblDistance = (UILabel*)[cell viewWithTag:102];
UILabel *lbltype = (UILabel*)[cell viewWithTag:103];
lblDate.text = [temp objectAtIndex:indexPath.row] valueForKey:#"date"] stringByReplacingOccurrencesOfString:#"-" withString:#"/"]];
lblDistance.text = [NSString stringWithFormat:#"%# KM",[[temp objectAtIndex:indexPath.row] valueForKey:#"distance"]];
NSString *type = [NSString stringWithFormat:#"%#",[[temp objectAtIndex:indexPath.row] valueForKey:#"distance"]];
if([type isEqualToString:#"0"])
{
lbltype.text = #"Personal";
}
else
{
lbltype.text = #"Bussiness";
}
// Configure
return cell;
}
I have a UITableView that uses the UITableViewCellStyleDefault style. I also have set the detail accessory:
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
I want the code to be so that when the detail accessory is push, thus generating the delegate method accessoryButtonTappedForRowWithIndexPath: , the image of that particular cell is changed to another image.
How can I do that? Do I need to recreate the cell somehow?
Thank you!
no you have to just add the other image in the cell's imageview as
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"IMAGENAME"];
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell = [tableView cellForRowAtIndexPath:indexPath];
// Now You have your sekected cell, you can set the image which you have set on this cell
UIImageView *aView = (UIImageView*)[aCell viewWithTag:1];
// Now change the image of aView
}
In your accessoryButtonTappedForRowWithIndexPath method use --
UITableViewCell *cell = [tableView.delegate cellForRowAtIndexPath:indexPath];
cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]] autorelease];
You change the image of your underlying data structure. After that, reload the cell at the appropiate index path.
For example, I assume your data is a simple NSArray with NSDictionary objects in it. The dictionary contains a few keys, like "text" and "image". In cellForRowAtIndexPath: you would use the values in the appropiate dictionary to set cell.text and cell.image.
So, in accessoryButtonTappedForRowWithIndexPath: (or maybe you want didSelectRowAtIndexPath:?), you would do something like that:
NSDictionary* dict = [myArray objectAtIndex:indexPath.row];
[dict setValueForKey:#"image":myNewImage];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationAutomatic];
Bear in mind that's pseudo-code, not tested, but will give you the idea how to do it.
i've never done that but id try something like
-accessoryButtonTappedForRowWithIndexPath..{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// change the view of cell here
}
just an idea, please add some code of what you've tried so far in your question.
Normally in a UITableView, we assign the value for the cells in the cellForRowAtIndexPath method which is the delegate method of UITableView. In this case, if we use a CustomTableViewCell, we assign the values in cellForRowAtIndexPath as
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomTableViewCell" owner:self options:nil];
cell = customTableViewCell;
}
cell.label1.text = #"text1";
cell.label2.text = #"text2";
// Configure the cell...
return cell;
}
This way of using CustomCell is a usual one. The texts will be displayed on the labels of the cell, but I need some clarification in using CustomCell in another way. I have a method in MainViewController like the following
- (void)methodInMainViewConroller {
CustomCell *customCell = [[CustomCell alloc] init];
[customCell methodInCustomCell];
[tableView reloadData];
}
and in CustomCell, I have the definition for the method as
- (void)methodInCustomCell {
self.label1.text = #"text1";
self.label2.text = #"text2";
}
Will this work? Will the text be displayed on the labels of the cell? Actually it doesn't work for me. Anyone help me to make this work perfectly. Thanks in advance.
You can try this
- (void)methodInMainViewConroller {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
CustomCell *customCell = (CustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[customCell methodInCustomCell];
}
UPDATE: What I've just realized is that once you scroll the table view your labels will be reset to previous values. It might not work this way, but give it a try.
Take a good look at your code. You never actually do anything with that cell once you set the labels. Anyway, how would the tableview know to use your cell and where to put it?
It doesn't work because the cell you alloc/init and work on is a totally different object. You can, however, store that cell as an ivar and always return that very cell for the given indexPath.
This method don't work because you call it for just created custom cell not for cell in tableView.
You need something like this:
CustomCell *customCell = [tableView cellForRowAtIndexPath:indexPath];
[customCell methodInCustomCell];