i have one view controller
Name : FirstVC
i have table view in that view controller. in my table view i load a custom cell. in my custom cell there is 1 label and 1 button. i set label as well as button of custom cell from FirstVC. See bellow code.
in FirstVC.h
#interface FirstVC : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
IBOutlet UITableView *TblView;
IBOutlet CustomCell *customCell;
}
- (void)addCounter:(NSInteger)pCat_ID;
#end
in FirstVC.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil){
[[NSBundle mainBundle] loadNibNamed:#"CustomCell_iPhone" owner:self options:nil];
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell = categoryCustomCell;
}
NSInteger pCat_id = [[[arrayCategoryData objectAtIndex:indexPath.row] objectForKey:#"cat_id"] integerValue];
[customCell setTextForLable:#"Test"];
[customCell setAddCounterButton:pCat_id];
return cell;
}
- (void)addCounter:(NSInteger)pCat_ID
{
//Some code hereā¦.
}
in CustomCell.h
#interface CustomCell : UITableViewCell
{
IBOutlet UILabel *lblCategoryName;
IBOutlet UIButton *btnAddCounter;
}
- (void)setTextForLable:(NSString *)cat_name;
- (void)setAddCounterButton:(NSInteger)cat_ID;
#end
in CustomCell.m
#implementation CustomCell
- (void)setTextForLable:(NSString *)cat_name
{
lblCategoryName.text = cat_name;
}
- (void)setAddCounterButton:(NSInteger)cat_ID
{
[btnAddCounter addTarget:self action:#selector(addCounter:cat_ID) forControlEvents:UIControlEventTouchUpInside];
}
#end
i want to call addCounter:(NSInteger)pCat_ID method from custom cell. but this idea can not worked. Please suggest me new idea.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil){
[[NSBundle mainBundle] loadNibNamed:#"CustomCell_iPhone" owner:self options:nil];
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell = categoryCustomCell;
}
NSInteger pCat_id = [[[arrayCategoryData objectAtIndex:indexPath.row]
// call your method from here.. and put your method in this file then it will work for you
[cell.btnAddCounter addTarget:self action:#selector(addCounter:pCat_id) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
Related
Please help me with UITableViewCells.
I have followed many tutorials and sites but didn't get my problem solved.
I have tried to make a simpleCustomCells app but data are not loaded in the table view.
1.I have a table view controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [NSArray arrayWithArray:[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil]];
for(id obj in nib)
{
if([obj isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell *)obj;
}
}
}
cell.label.text = #"hello";
return cell;
}
Also tried
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
"within if(cell==nill)"
2.I have created CustomCell : UITableViewCell with only a UILabel outlet. Changed CustomCell.xib identifier's name.
When I run this app I get this kind of error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x71627f0> setValue:forUndefinedKey:]: this class
is not key value coding-compliant for the key label.
Please Help me with this issue, where am I going wrong.
Ok, I think I got where you are getting stuck.
Step 1 : Disconnect the outlet UILabel from the xib file.
Step 2 : Go to your xib file and you will see your objects list on the left hand side of the xib file.
Step 3 : Take your mouse pointer just below where objects is written. You will find Custom Cell written.
Step 4 : Press ctrl and drag it to the label and now Connect the Outlet to label.
Step 5 : Clean your program and Run it again.
All the best. If you still have doubts, I will elaborate. :)
I think you should add like this, I hope it works
static NSString *CellIdentifier = #"Cell";
customcellCell *cell = (customcellCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; <- set your customclass in this line.
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"customviewcell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.label.text = #"hello";
return cell;
Use following way :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ContactListCell *contactCell;
NSString *contactCellID = [NSString stringWithFormat:#"ContactCell%d%d",indexPath.row,indexPath.section];
contactCell = (ContactListCell *)[tableView dequeueReusableCellWithIdentifier:contactCellID];
if(!contactCell) {
contactCell = [[ContactListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contactCellID];
contactCellID = nil;
}
strName = #"First name";
contactCell.lblFirstName.text = [strName capitalizedString];
return contactCell;
}
For Custom cell .h file
#interface ContactListCell : UITableViewCell {
UILabel *lblFirstName;
}
#property (strong, nonatomic) UILabel *lblFirstName;
#end
For Custom cell .m file
#implementation ContactListCell
#synthesize lblFirstName;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
lblFirstName = [[UILabel alloc]initWithFrame:CGRectMake(kxiPadFirstNameCellLandscape, kyiPadFirstNameCellLandscape, kiPadFirstNameCellWidthLandscape, kiPadFirstNameCellHeightLandscape)];
lblFirstName.backgroundColor= [UIColor clearColor];
lblFirstName.numberOfLines = 0;
lblFirstName.textColor = kCustomCellFontColor;
[self.contentView addSubview:lblFirstName];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Why not give you're cell an identifier and instantiate it with dequewithreusableidentifier?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[AanbodTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.Label.text =#"Hello"
return cell;
}
i would also refer to this very good tutorial http://www.appcoda.com/customize-table-view-cells-for-uitableview/
I have a button in each cell in my tableview. Basically I'm saving the cell's indexPath in the button's tag in thecellForRowAtIndexPath` method like this:
- (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];
}
UIButton *expandPhoto = (UIButton *)[cell viewWithTag:101];
expandPhoto.tag = indexPath.row;
[expandPhoto addTarget:self action:#selector(expand:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
The problem is, when i scroll down the button's indexPath resets.
I've searched alot in the internet but I'm stuck with this problem for days.
You are using static NSString *CellIdentifier = #"Cell"; it will reuse the cells so you will get the cell index as 0 when it should show 6, shows 1 when it should show 7, etc..
If you want to get the index as 0,1,....6,7, etc..
Use Like this,
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d",indexPath.row];
But it wont reuse the cell.
I solved my issue using this code:
- (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];
}
UIButton *expandPhoto = (UIButton *)[cell viewWithTag:101];
[expandPhoto setTag:indexPath.row];
[expandPhoto addTarget:self action:#selector(expand:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (IBAction)expand:(id)sender {
UIButton *btn = (UIButton*) sender;
UITableViewCell *cell = (UITableViewCell*) btn.superview.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
int row = indexPath.row;
}
Thanks.
Custom table view cell is the best approuch to solve this problem here we need to create different the Custom table view cell controller and use this in cellForAtIndexPath tableview Datasource :
CustomTableViewCell.h
#import <UIKit/UIKit.h>
#interface CustomTableViewCell : UITableViewCell
#property (nonatomic, readonly) UILabel *tblViewLabel;
#property (nonatomic, readonly) UIButton *tblViewBtn;
#end
CustomTableViewCell.m
#import "CustomTableViewCell.h"
#implementation CustomTableViewCell
#synthesize tblViewLabel = _tblViewLabel;
#synthesize tblViewBtn = _tblViewBtn;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString
*)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
_tblViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 50, 30)];
[self.contentView addSubview:_tblViewLabel];
[_tblViewLabel release];
_tblViewBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_tblViewBtn.frame = CGRectMake(100, 5, 200, 20);
[_tblViewBtn.titleLabel setText:[NSString stringWithFormat:#"%d",self.tag]];
[_tblViewBtn addTarget:self action:#selector(tblBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:_tblViewBtn];
[_tblViewBtn release];
}
return self;
}
- (void)tblBtnPressed :(UIButton *)btn {
NSLog(#"Here %d button is pressed.",btn.tag);
}
- (void)dealloc {
[_tblViewLabel release];
[_tblViewBtn release];
[super dealloc];
}
#end
//------------------------//
Here In Your Previous controller's cellForRowAtIndexPath datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = #"MyIdentifier";
CustomTableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier] ;
if (cell == nil){
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
cell.tblViewLabel.text =[NSString stringWithFormat:#"%d",indexPath.row];
[cell.tblViewBtn setTitle:[NSString stringWithFormat:#"%d",indexPath.row] forState:UIControlStateNormal];
cell.tblViewBtn.tag = indexPath.row;
return cell;
}
I need to pass an string value from UItable View Controller to Uitable view cell
Here is the code which i used
In my Table view controller
#import <UIKit/UIKit.h>
#import "CustomCell.h"
#interface DetViewController : UITableViewController
{
NSString *urlstring;
CustomCell *cust;
}
#property(nonatomic,retain)NSString *urlstring;
#property(nonatomic,retain)NSMutableArray *mutarray;
#property(nonatomic,retain)CustomCell *cust;
#end
and im TableViewController.m have
- (void)viewDidLoad
{
[super viewDidLoad];
url=[NSURL URLWithString:urlstring];
urldata=[[NSString alloc]initWithContentsOfURL:url];
mutarray=[[NSMutableArray alloc]init];
self.mutarray=[urldata JSONValue];
NSDictionary *dict=[mutarray objectAtIndex:0];
self.title=[dict valueForKey:#"category"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell =(CustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.custurlstring=urlstring;
NSLog(#"%#", cell.custurlstring);
return cell;
}
In my Custom Tableviewcell.h have
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell{
NSString *custurlstring;
}
#end
My custom Tableviewcell.m have
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSLog(#"%#",custurlstring);
}
return self;
}
Now i got the string content in tableviewcontroller by using NSLog..but if i tried to print the value in TableViewCell i cant get those values...
Guidance Please...
No need of customizing a cell for displaying a image and labels on it,
Just add them in normal cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:<style enum value> reuseIdentifier:reuseIdentifier string:urlstring]
//cell.mystring = self.mystring;
}
// customize your image and your labels here with rects here
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"yourimage.png"]];
[cell.contentView addSubview:image];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(your rect here)];
[cell.contentView addSubview:label];
return cell;
}
You need to setup your cell in cellForRowAtIndexPath: and then set the string on the cell inside this method. This method is already provided to you when you create a UITableViewController.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.custurlstring = urlstring;
return cell;
}
You should do it in delegate method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You can write something like that:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:<style enum value> reuseIdentifier:reuseIdentifier string:urlstring]
//cell.mystring = self.mystring;
}
return cell;
}
Added:
You can write custom constructor and initialize the cell using it. This way NSLog will print the string as expected:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier string:(NSString *)string
{
self = [self initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.custurl=[NSURL URLWithString:string];
NSLog(#"%#",custurl);
}
return self;
}
In my app I have custom UITableViewCell, and I have UIStepper and UILabel in the custom cell. I don't know how to check which stepper was clicked. So is it a way to know from which cell stepper was clicked?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
if ([nib count] > 0) {
cell = self.tbcell;
}
}
return cell;
}
An alternative is:
In the method that gets fired when the UIStepper value changes (e.g. #max_, above), you can do the following:
- (IBAction)stepperValueDidChanged:(UIStepper *)sender {
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
// assuming your view controller is a subclass of UITableViewController, for example.
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
[step addTarget:self action:#selector(someAction:) forControlEvents:UIControlEventValueChanged];
- (void) someAction:(UIStepper *) stepper {
NSLog(#"stepper clicked");
}
My Custom View Cell looks like the following http://img34.imageshack.us/i/screenshot20110210at627.png/
#interface AddSiteAddressCell : UITableViewCell {
IBOutlet UITextField *street;
IBOutlet UITextField *city;
IBOutlet UITextField *province;
IBOutlet UITextField *postal;
IBOutlet UITextField *country;
IBOutlet UITextField *siteName;
}
#property (nonatomic,retain)UITextField *siteName;
#end
#implementation AddSiteAddressCell
- (void) textFieldDidEndEditing:(UITextField *)textField {
NSLog(#"%#",street);
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
}
#end
AddSiteViewController.m
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0)
{
static NSString *MyIndentifier = #"MyIdentifier";
MyIndentifier = #"tblCellView";
//static NSString *CellIdentifier = #"Cell";
AddSiteAddressCell *cell = (AddSiteAddressCell *)[add_site dequeueReusableCellWithIdentifier:MyIndentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"AddSiteAddressCell" owner:self options:nil];
cell = nameCell;
}
return cell;
}else if (indexPath.section == 1)
{
static NSString *MyIndentifier = #"MyIdentifier";
MyIndentifier = #"tblCellView";
//static NSString *CellIdentifier = #"Cell";
AddSiteAddressCell *cell = (AddSiteAddressCell *)[add_site dequeueReusableCellWithIdentifier:MyIndentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"AddSiteAddressCell" owner:self options:nil];
cell = addressCell;
}
return cell;
}
/*static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...*/
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
AddSiteAddressCell *cell = (AddSiteAddressCell *)[add_site cellForRowAtIndexPath:[indexPath row]];
self.test = cell.siteName.text;
NSLog(#"%#",self.test);
}
MY full view looks like this:
http://img29.imageshack.us/i/screenshot20110211at940.png/
This is what my View looks like. 2 sections with 1 row each and 1 custom cell each
Thats code from my CustomTableViewCell
I am wondering how I can access my text field in my table controller to save the data that is entered
when do u want to access it?
you should make property for your text field (e.g siteName), and then if you have the IndexPath of your cell, u can use:
AddSiteAddressCell *myCell=(AddSiteAddressCell *)[tableView cellForRowAtIndexPath:MyIndexPath];
myCell.siteName....