- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"NotificationViewCell";
CardViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CardViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.contentView.backgroundColor=[UIColor clearColor];
[ModelClass addSublayer:cell.contentView];
cell.cellbg.layer.cornerRadius = 8;
cell.cellbg.layer.masksToBounds = YES;
cell.cellbg.layer.borderWidth = 1;
cell.cellbg.layer.borderColor = [[UIColor grayColor] CGColor];
cell.logoImage.layer.cornerRadius = 8;
cell.logoImage.layer.masksToBounds = YES;
cell.logoImage.layer.borderWidth = 1;
cell.logoImage.layer.borderColor = [[UIColor grayColor] CGColor];
Merchant *merchantList= [self.cardListArr objectAtIndex:indexPath.row];
cell.nameLab.text=merchantList.name;
NSLog(@"merchantList.myloyalty.points=%@",merchantList.myloyalty.points);
// NSLog(@"memberNO=%@",merchantList.myloyalty.memberNO);
cell.integralLab.text=[NSString stringWithFormat:NSLocalizedString(@"points_dot", @"") ,[merchantList.myloyalty.points intValue]];
cell.cardNumberLab.text=[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"ID", nil), merchantList.myloyalty.memberNO];
if(![ModelClass isBlankString:merchantList.introPic])
{
NSLog(@"merchantList.introPic=%@",merchantList.introPic);
[cell.logoImage setImageUrl:merchantList.introPic];
}
}
return cell;
}
你可以看到我使用 if(cell==nil) 的代码,我想知道我应该写什么代码 if(cell!=nil)(if(cell==nil) else{ 我应该做什么什么可以提高细胞复用})
如果每个单元格的 View 相同,但高度不同,例如imageview有时是20或40等,如何处理这种情况。
最佳答案
1.这是不正确的,因为你的单元格被重用了,当它被创建时,它不会进入if-statement
,所以,在if-statement
你只需要初始化 cell ,你应该在 if-statement
之外编码的 setText 和 setImage 。
比如:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"NotificationViewCell";
CardViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CardViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.contentView.backgroundColor=[UIColor clearColor];
[ModelClass addSublayer:cell.contentView];
cell.cellbg.layer.cornerRadius = 8;
cell.cellbg.layer.masksToBounds = YES;
cell.cellbg.layer.borderWidth = 1;
cell.cellbg.layer.borderColor = [[UIColor grayColor] CGColor];
cell.logoImage.layer.cornerRadius = 8;
cell.logoImage.layer.masksToBounds = YES;
cell.logoImage.layer.borderWidth = 1;
cell.logoImage.layer.borderColor = [[UIColor grayColor] CGColor];
}
Merchant *merchantList= [self.cardListArr objectAtIndex:indexPath.row];
cell.nameLab.text=merchantList.name;
NSLog(@"merchantList.myloyalty.points=%@",merchantList.myloyalty.points);
// NSLog(@"memberNO=%@",merchantList.myloyalty.memberNO);
cell.integralLab.text=[NSString stringWithFormat:NSLocalizedString(@"points_dot", @"") ,[merchantList.myloyalty.points intValue]];
cell.cardNumberLab.text=[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"ID", nil), merchantList.myloyalty.memberNO];
if(![ModelClass isBlankString:merchantList.introPic])
{
NSLog(@"merchantList.introPic=%@",merchantList.introPic);
[cell.logoImage setImageUrl:merchantList.introPic];
}
return cell;
}
2 大多数人的代码是这样的:
if(cell==nil)
{
//init code
}
// setting code
3.如果要设置单元格高度,则不能在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
你应该在数据源的方法中编码:- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
代码如下:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (index.row % 2)
return 20.0f;
return 40.0f;
}
关于iphone - UITableViewCell 重用良好实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15106373/
嗨,我正在考虑开发一种文件传输程序,想知道我是否想要尽可能好的加密,我应该使用什么? 我会用 C# 开发它,所以我可以访问 .net 库 :P在我的 usb 上有一个证书来访问服务器是没有问题的,如果
我创建的这个计算两个数组的交集是线性的方法的复杂度(在良好、平均、最差的情况下)? O(n) public void getInt(int[] a,int[] b){ int i=0; int
我已经能够使用 RTCPeerConnection.getStats() API 获得 WebRTC 音频调用的各种统计信息(抖动、RTT、丢包等)。 我需要将整体通话质量评为优秀、良好、一般或差。
基本问题: 如果我正在讲述/修改数据,我应该通过索引硬编码索引访问文件的元素,即 targetFile.getElement(5);通过硬编码标识符(内部翻译成索引),即 target.getElem
在 Linux 上,我想知道要调用什么“C”API 来获取每个 CPU 的统计信息。 我知道并且可以从我的应用程序中读取 /proc/loadavg,但这是系统范围的负载平均值,而不是每个 CPU 的
在客户端浏览器中使用 fetch api,GET 或 POST 没有问题,但 fetch 和 DELETE 有问题。它似乎将 DELETE 请求方法更改为 OPTIONS。 大多数研究表明是一个cor
我是一名优秀的程序员,十分优秀!