(自定义)滚动后UITableViewCell的混合
我已经在这个问题上几个星期了。(自定义)滚动后UITableViewCell的混合
基本上当我向上滚动的TableView塔尔中/下使用在IB设计了一个自定义单元格的所有内容都混合起来,放错地方的
香港专业教育学院尝试多种解决方案,但都无济于事,你要去必须原谅我的代码一点点。
人们总是建议为表格单元格创建一个子视图,但我不知道该怎么做= /对于iOS开发还是一个相当新的东西,所以如果你有一个可能的答案,你能否尽可能的详细说明一下。
再次,对不起,我的代码=/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
NSInteger intCellTag;
NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
[[[NSBundle mainBundle] loadNibNamed:@"EventsCustomTVCell" owner:self options:nil] lastObject];
cell = tvCell;
self.tvCell = nil;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.tag = intCellTag;
intCellTag++;
UIImage *customCellBG = [UIImage imageNamed:@"EventsCustomTableCellBG.png"];
UIImageView *customCellBGImageView = [[UIImageView alloc] initWithImage: customCellBG];
customCellBGImageView.contentMode = UIViewContentModeScaleToFill;
cell.backgroundView = customCellBGImageView;
[customCellBGImageView release];
[cell.contentView addSubview:imgThumbnail];
[cell.contentView addSubview:lblName];
[cell.contentView addSubview:lblDescription];
[cell.contentView addSubview:lblDate];
}
imgThumbnail.image = [UIImage imageNamed:[dictionary objectForKey: @"Thumbnail"]];
lblName.text = [dictionary objectForKey:@"Title"];
lblDescription.text = [dictionary objectForKey:@"Description"];
lblDate.text = [dictionary objectForKey:@"Date"];
return cell;
}
它看起来像你试图混合隐喻来定义每个UITableViewCell - 从.xib加载,并手动创建子视图。没有错,当然,但你可以直接把图像和标签进入tableviewCell,就像这样:
和这里的显示每行代码(自然在IB您分配非零个独特的标签给每个对象的UIKit要自定义在每行的基础上)
#define kImageTag 1
#define kLabel1Tag 2
#define kLabel2Tag 3
#define kLabel3Tag 4
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MyTvCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
self.tvCell = nil;
[[NSBundle mainBundle] loadNibNamed:@"TvCell" owner:self options:nil];
cell = self.tvCell;
}
UIImageView *iv = (UIImageView *) [cell viewWithTag:kImageTag];
UILabel *lbl1 = (UILabel *) [cell viewWithTag:kLabel1Tag];
UILabel *lbl2 = (UILabel *) [cell viewWithTag:kLabel2Tag];
UILabel *lbl3 = (UILabel *) [cell viewWithTag:kLabel3Tag];
iv.image = [UIImage imageNamed:@"myimage.png"];
lbl1.text = @"howdy";
lbl2.text = @"there";
lbl3.text = @"foo";
return cell;
}
你加入同一子视图对象,每个单元
[cell.contentView addSubview:imgThumbnail];
[cell.contentView addSubview:lblName];
[cell.contentView addSubview:lblDescription];
[cell.contentView addSubview:lblDate];
每个细胞都需要自己的一组子视图对象。
imgThumbnail
lblName
lblDescription
lblDate
您应该为这些对象分配一个标记(您可以在界面构建器中执行此操作)。
而且,如果您配置单元格,则可以查询标记的单元格并设置其属性。
现在您保留对最后添加的单元格的标签的引用,并且每次表格要求您更改这些标签的新单元格时。
比方说,你在InterfaceBuilder中指定标签10到标签lblDescription
那么您需要更换
lblDescription.text = [dictionary objectForKey:@"Description"];
与
UILabel *lblDescription = (UILabel *)[cell viewWithTag:10];
lblDescription.text = [dictionary objectForKey:@"Description"];
编辑:我认为imgThumbnail等都是子视图您单元格,但是你再次添加它们。如果我的假设是正确的,你应该摆脱[cell.contentView addSubview ...]。
如果我错了,你应该摆脱imgThumbnail等作为您的viewcontroller的实例变量。每次创建新单元格时添加单独的UIViews。就像你用backgroundview做的一样。但是当您配置单元格值时,您已分配标签并使用该标签。
您可以使用
的NSString * cellIdentifier = [的NSString stringWithFormat:@ “%d”,indexPath。行];
代替
静态的NSString * CellIdentifier = @ “小区”;
iOS Swift: I have done following to resolve my issue
let CellIdentifier: String = "CellIdentifier\(indexPath.section)\(indexPath.row)"
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as UITableViewCell?
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: CellIdentifier)
}
这工作就像一个魅力! – Andyy 2011-02-18 02:54:49