(自定义)UITableViewCell在滚动后混音 [英] (custom) UITableViewCell's mixing up after scrolling
问题描述
我已经解决了这个问题几周了。
Ive been on this problem for a few weeks now.
基本上当我在TableView中向上/向下滚动时,thar使用IB中设计的自定义单元格所有内容混乱和错位
Basically when i scroll up/down within a TableView thar uses a Custom Cell designed in IB all the content gets mixed up and misplaced
我尝试了多种解决方案,但无济于事,你将不得不原谅我的代码。
Ive tried multiple solutions but to no avail, your gonna have to excuse my code a little bit.
人们一直建议为表格单元格制作一个子视图,但我不知道如何做到这一点= /仍然是iOS开发的新手,所以如果你有一个可能的答案,你能尽可能详细说明吗。
People keep suggesting to make a subView for the table cell but i have no idea how to do that =/ still quite new to iOS development so if you have a possible answer, can you detail it as much as possible please.
再一次,抱歉我的代码= /
Once again, sorry for my code =/
- (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,如下所示:
It looks like you're trying to mix metaphors in defining each UITableViewCell -- loading from .xib, and creating subviews manually. Nothing wrong with this of course, but you could put the image and labels into the tableviewCell directly, like this:
这里是显示每一行的代码(当然在IB中你已经指定了非每个UIKit对象的-zero唯一标记,您希望按行自定义)
and here's the code to display each row (naturally in IB you've assigned non-zero unique tags to each UIKit object you want to customize on a per-row basis)
#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;
}
这篇关于(自定义)UITableViewCell在滚动后混音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!