ios - 如何使用 UItableViewCell 重用标识符
<p><p>我想使用 <code>UITableviewCell</code> 重用,我有一个自定义 <code>UITableViewCell</code> 并且使用 ARC,我的 <code>UITableViewCell</code> 是不同的。我的代码是:</p>
<pre><code> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PostCount *post=;
static NSString *CellIdentifier = @"TimeLineViewCell";
TimeLineViewCell *cell = (TimeLineViewCell*);
if(cell==nil){
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier11"];
}
cell.tag=indexPath.row+1000;
cell.lab_time.text=;
cell.delegate=self;
;
post=nil;
return cell;
}
</code></pre>
<p>你知道我是否使用</p>
<pre><code>cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
</code></pre>
<p>改为</p>
<pre><code>cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier11"];
</code></pre>
<p>我的手机总是在错误的位置。为什么我不能使用相同的 <code>reuseIdentifier</code>。如果我使用不同的重用标识符,如果我的单元格没有被重用。</p>
<p>对于其他 ViewcontrollerView ,当单元格的高度相同并且我使用相同的 <code>reuseIdentifier</code> 时,一切正常。谁能帮我解决这个问题?</p>
<p><strong>编辑:</strong>我的自定义单元格代码:</p>
<pre><code>#import "TimeLineViewCell.h"
@implementation TimeLineViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = ;
if (self) {
imageView_t=[initWithFrame:CGRectMake(5, 5, 40, 40)];
imageView_t.hidden=YES;
imageIcon=[initWithFrame:CGRectMake(295, 5, 16, 16)];
self.lab_time = [ initWithFrame:CGRectMake(210, 5, 80, 20)];
self.lab_time.textColor=;
self.lab_time.font=;
self.lab_time.textAlignment=UITextAlignmentRight;
self.lab_time.backgroundColor=;
whoscreenName = [ initWithFrame:CGRectMake(60, 5, 145, 20)];
whoscreenName.textColor=;
whoscreenName.font=;
whoscreenName.lineBreakMode = UILineBreakModeTailTruncation;
whoscreenName.numberOfLines = 0;
whoscreenName.backgroundColor=;
myWhat = [ init];
myWhat.textColor=;
myWhat.backgroundColor=;
;
;
myWhat.dataDetectorTypes=UIDataDetectorTypeLink;
;
self.myWhaticon=[init];
myImageView=[init];
myImageView.hidden=YES;
myIndicatorView=[initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
myIndicatorView.hidden=YES;
whoBgView=[init];
whoBgView.image = [ stretchableImageWithLeftCapWidth:130 topCapHeight:7];
whoWhat = [ init];
whoWhat.textColor=;
whoWhat.backgroundColor=;
;
;
whoWhat.dataDetectorTypes=UIDataDetectorTypeLink;//url
;
whoImageView=[init];
whoImageView.hidden=YES;
activityIndicatorView=[initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicatorView.hidden=YES;
myWhat.font=;
whoWhat.font=;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
;
// Configure the view for the selected state
}
- (void)layoutSubviews
{
;
;
;
;
;
;
;
;
;
;
;
;
;
whoImageView=nil;
whoWhat=nil;
}
-(void)my_imag_btnClick:(UIButton *)btn{
;
}
-(void)who_imag_btnClick:(UIButton *)btn{
NSLog(@"who_imag_btnClick");
;
}
- (void)dealloc
{
imageView_t.image=nil;
imageView_t=nil;
imageIcon.image=nil;
imageIcon=nil;
whoscreenName=nil;
self.lab_time=nil;
whoscreenName=nil;
myWhat=nil;
self.myWhaticon=nil;
myImageView.image=nil;
myImageView=nil;
whoBgView.image=nil;
whoBgView=nil;
whoImageView.image=nil;
whoImageView=nil;
}
-(void)setViewStyle:(PostCount *)post{
//时间
if (self.btime==1) {
self.lab_time.text=;
}else{
self.lab_time.text=;
}
imageView_t.hidden=NO;
// imageView_t.isRoundIcon=YES;
imageView_t.layer.cornerRadius = 5;
imageView_t.layer.masksToBounds = YES;
//给图层添加一个有色边框
imageView_t.layer.borderWidth = 0.8;
imageView_t.layer.borderColor = [ CGColor];
if(!)
{
placeholderImage:];
}
else{
imageView_t.image=;
}
//icon
imageIcon.image=];
whoscreenName.text=post.who.screenName;
CGSize size_myWhat = WithText:post.what weith:250];
myWhat.frame=CGRectMake(50, 25, 250, size_myWhat.height);
myWhat.text=;
self.myWhaticon.frame=myWhat.frame;
if(!)
{
CGSize size_myImageView=CGSizeMake(80, 80);
myImageView.frame=CGRectMake(50, 30+size_myWhat.height, size_myImageView.width, 80);
myImageView.hidden=NO;
myIndicatorView.center=myImageView.center;
myIndicatorView.hidden=NO;
;
__block UIActivityIndicatorView *indicatorView=myIndicatorView;
__block UIImageView *myImage = myImageView;
placeholderImage:nil
success:^(UIImage *image){
CGRectsFrame=myImage.frame;
//缩放
CGSize newSize=image.size;
if (newSize.height>80) {
newSize.height=80;
newSize.width=newSize.width*80.0/image.size.height;
}else{
if (newSize.width>80) {
newSize.height=newSize.height *80.0/image.size.width;
newSize.width=80;
}else{//2个都小于80
newSize.height=newSize.height;
newSize.width=newSize.width;
}
}
sFrame.size=newSize;
myImage.frame=sFrame;
indicatorView.hidden=YES;
;
;
}
failure:^(NSError *error){
indicatorView.hidden=YES;
;
;
}];
UIButton * myImageBtn=[init];
myImageBtn.frame= CGRectMake(50, 30+size_myWhat.height, size_myImageView.width, 80);
;
myImageBtn.tag=self.tag-1000;
// myImageBtn.backgroundColor=;
;
}
if (>0) {
whoWhat.text=;
}
CGSize size_whoWhat = WithText:whoWhat.text weith:250];
whoWhat.frame=CGRectMake(10, 10, 250, size_whoWhat.height);
if(!)
{
CGSize size_whoImageView=CGSizeMake(80, 80);
whoImageView.frame=CGRectMake(30, 15+size_whoWhat.height, size_whoImageView.width, 80);
whoImageView.hidden=NO;
activityIndicatorView.center=whoImageView.center;
activityIndicatorView.hidden=NO;
;
__block UIActivityIndicatorView *indicatorView = activityIndicatorView;
__block UIImageView *whoImage = whoImageView;
placeholderImage:nil
success:^(UIImage *image){
CGRectsFrame=whoImage.frame;
//缩放
CGSize newSize=image.size;
if (newSize.height>80) {
newSize.height=80;
newSize.width=newSize.width*80.0/image.size.height;
}else{
if (newSize.width>80) {
newSize.height=newSize.height *80.0/image.size.width;
newSize.width=80;
}else{//2个都小于80
newSize.height=newSize.height;
newSize.width=newSize.width;
}
}
sFrame.size=newSize;
whoImage.frame=sFrame;
indicatorView.hidden=YES;
;
;
}
failure:^(NSError *error){
indicatorView.hidden=YES;
;
;
}];
UIButton * whoImageBtn=[init];
int myimageHeight=0;
if (myImageView.frame.size.height>0) {
myimageHeight=5+80;
}
whoImageBtn.frame=CGRectMake(30+42, 40+size_myWhat.height+size_whoWhat.height+myimageHeight, 80, 80);
whoImageBtn.backgroundColor=;
;
whoImageBtn.tag=self.tag-1000;
;
}
if (size_whoWhat.height<20) {
whoBgView.hidden=YES;
}else{
int myimageHeight=0;
if (myImageView.frame.size.height>0) {
myimageHeight=5+80;
}
whoBgView.frame=CGRectMake(42, 25+size_myWhat.height+myimageHeight, 270, 20+size_whoWhat.height+whoImageView.frame.size.height);
}
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>要么不要重复使用单元格,要么给每个单元格一个自定义标识符。</p>
<p>对于自定义标识符,替换为:</p>
<pre><code>static NSString *CellIdentifier = @"TimeLineViewCell";
TimeLineViewCell *cell = (TimeLineViewCell*);
if(cell==nil){
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier11"];
}
</code></pre>
<p>用这个:</p>
<pre><code>TimeLineViewCell *cell = (TimeLineViewCell*)];
if(cell==nil){
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:];
}
</code></pre>
<p>否则,就不要出队,每次都创建一个新单元格。</p>
<p>注意:基于单元格 indexPath.row 的自定义标识符仅在用户未进行表格编辑时才有效。如果他们将编辑表格,请不要重复使用单元格。</p></p>
<p style="font-size: 20px;">关于ios - 如何使用 UItableViewCell 重用标识符,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/15378522/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/15378522/
</a>
</p>
页:
[1]