Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 配置自定义UITableViewCell时如何传递参数_Ios_Objective C_Uitableview_Custom Cell - Fatal编程技术网

Ios 配置自定义UITableViewCell时如何传递参数

Ios 配置自定义UITableViewCell时如何传递参数,ios,objective-c,uitableview,custom-cell,Ios,Objective C,Uitableview,Custom Cell,我有一个自定义的UITableViewCell,我们称它为CustomCell。我只需要在创建参数时传递一个参数,比如说一个NSURL 这就是我在我的ViewController中所做的: 在viewDidLoad中: [myTableView registerClass: [CustomCell class] forCellReuseIdentifier: @"CustomItem"] 在tableView:cellforrowatinexpath: static NSString *myI

我有一个自定义的
UITableViewCell
,我们称它为
CustomCell
。我只需要在创建参数时传递一个参数,比如说一个
NSURL

这就是我在我的
ViewController
中所做的:

viewDidLoad
中:

[myTableView registerClass: [CustomCell class] forCellReuseIdentifier: @"CustomItem"]
tableView:cellforrowatinexpath:

static NSString *myIdentifier = @"CustomItem"
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier] 

所有这些都非常有效,但我需要使用这个
NSURL
配置单元格,但只需一次,而不是每次调用
cellforrowatinexpath
。我注意到,
CustomCell
中的
initWithStyle:reuseIdentifier:
只被调用了一次,但是我如何才能通过这个调用添加
NSURL

UITableViewCell
相同类型的UITableViewCell应该使用
cellforrowatinexpath:
重用。如果您希望
属性保持不变,它应该位于a区域。如果您希望它更改每个单元格,它应该进入区域B

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CustomCell";
    CustomCell *cell = 
      [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault 
                                reuseIdentifier:CellIdentifier];
       // (A) everything inside here happens once 
       // for example

       cell.textLabel.backgroundColor = [UIColor redColor];
    }

    // (B) everything here happens every reuse of the cell
    // for example

    cell.textLabel.text = [NSString stringWithFormat:@"%li",
                            (long)indexPath.row];
    return cell;
}

您只需在
viewDidLoad
中加载
NSArray
。在
cellforrowatinexpath:
中,您从
NSArray
添加URL,并将其插入
UITableViewCell

static NSString *myIdentifier = @"CustomItem"
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier]
[cell.labelURL setText:_arrayURLs[indexPath.row]];
您需要做的一切就是向名为labelURLCustomCell添加一个新属性,并将其与
UIToryBoard
xib
文件中的
UILabel
连接


想想看,你不能只设定一次
UITableViewCell
将重用其
UITableViewCells
,并始终多次为同一对象设置另一个值。这就是为什么您必须这样做,而不是只做一次。

是否要在TableView的所有单元格中添加URL?是的,但每次都有不同的URL。您是否将URL保存在任何数组中?我可以访问cellForRowAtIndexPath中的URL,我需要设置一次,但只能设置一次,而不是每次调用cellForRowAtIndexPath时。这可能吗?设置一次请澄清?好吧,这更多的是回答我的问题:“你不能只设置一次”,很公平。URL的用途是加载图片,我希望CustomeCell在加载图片后执行UIViewAnimation,我该如何做?我有动画和加载等的所有代码,但现在每次调用CellForRowatineXpath时我都会加载图片:p您应该在后台使用
NSURLConnection sendAsynchronousRequest:queue:completionHandler:
执行此操作。您可以在CustomCell内创建一个方法,如果要选择某个单元格,请单击标签中的sell已知道其
NSURL
。如果你没有太多的图片,你也可以加载它们
asynchronous
,而
UITableViewController
将被加载并保存到字典中,这样一切都会变得更容易,但看起来你不只是在谈论10个“UIImages”,而且由于重复使用单元格的概念,细胞不可能有自己的变量,对吗?所以我真正需要的是让数据源处理这个问题?好的,因为这样重用它可能会很复杂。但是您知道
UITableViewCells
的顺序,因此您可以在
didSelectRowAtIndexPath
中获取所选行,并向特定单元格添加
UIImage
,因为[feedTableView registerClass:[CutomCellClass]forCellReuseIdentifier:@“CustomItem”],单元格永远不会为零,因此A区域永远不会被调用…在这种情况下,如果你正在创建静态单元格,看起来你是,听起来你可以把它放在这个方法中?