0

我需要将一个字符串值从 UItable 视图控制器传递给 UItable 视图单元

这是我在表视图控制器中使用的代码

#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

和我 TableViewController.m 有

- (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;
}

在我的自定义 Tableviewcell.h 中有

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell{
NSString *custurlstring;


}
@end

我的自定义 Tableviewcell.m 有

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {

  NSLog(@"%@",custurlstring);
}
return self;
}

现在我通过使用 NSLog 获得了 tableviewcontroller 中的字符串内容..但是如果我尝试在 TableViewCell 中打印值,我无法获得这些值...请指导...

4

3 回答 3

2

无需自定义单元格以在其上显示图像和标签,只需将它们添加到普通单元格中

- (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;
}
于 2012-07-24T11:05:50.563 回答
2

您需要设置您的单元格,cellForRowAtIndexPath:然后在此方法内的单元格上设置字符串。当您创建一个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;
}
于 2012-07-23T12:33:15.540 回答
1

您应该在委托方法中执行此操作 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

你可以这样写:

- (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;
}

补充:您可以编写自定义构造函数并使用它初始化单元格。这样 NSLog 将按预期打印字符串:

- (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;
}
于 2012-07-23T12:31:11.980 回答