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]];
您需要做的一切就是向名为labelURL的CustomCell添加一个新属性,并将其与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区域永远不会被调用…在这种情况下,如果你正在创建静态单元格,看起来你是,听起来你可以把它放在这个方法中?