Reputation: 2243
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...
Upvotes: 0
Views: 2075
Reputation: 4946
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;
}
Upvotes: 2
Reputation: 940
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;
}
Upvotes: 1
Reputation: 14886
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;
}
Upvotes: 2