簡體   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