繁体   English   中英

在iOS中将数据从UItableviewController传递到UItableViewcell

[英]Passing data from UItableviewController to UItableViewcell in ios

我需要将字符串值从UItable View Controller传递给Uitable视图单元格

这是我在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

和IM 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;
}

在我的Custom 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中打印该值,我将无法获得这些值。

您需要在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;
}

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

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

您应该在委托方法中进行-(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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM