[英]How restrict tableview to fill cell second time if it filled first time
在我的TableView中,我正在設置圖像和兩個標簽,它填充得很好。 但是,當我向下滾動TableView然后重新查看該已填充的單元格時,它將重新填充。 現在,我用來自URL的圖像填充ImageView,以便為用戶創建負載。
所以我可以限制我的TableView嗎,如果我的單元格第一次被填充,它將不會再次填充。
這是我的代碼:
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell * cell = [atableView dequeueReusableCellWithIdentifier: nil];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
ChannelList *channelList = nil;
if (searchedData.count ==0) {
channelList = [channelAllData objectAtIndex:[indexPath section]];
} else {
channelList = [searchedData objectAtIndex:[indexPath section]];
}
UIImageView *aImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 50, 50)];
aImageView.image = [UIImage imageNamed:@"tv-defolt.png"];
NSString * mystr = [channelList channelLogo];
if (mystr.length !=0) {
//get a dispatch queue
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//this will start the image loading in bg
dispatch_async(concurrentQueue, ^{
NSString * str = [NSString stringWithFormat:@"http://manektech.net/mttv/Channel/%@",[channelList channelLogo]];
NSData *image = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str]];
//this will set the image when loading is finished
dispatch_async(dispatch_get_main_queue(), ^{
aImageView.image = [UIImage imageWithData:image];
});
});
} else {
aImageView.image = [UIImage imageNamed:@"tv-defolt.png"];
}
[cell addSubview:aImageView];
UILabel *nameTextLabel = [[UILabel alloc] initWithFrame: CGRectMake(80, 15, 175, 20)];
nameTextLabel.textColor = [UIColor blackColor];
nameTextLabel.backgroundColor = [UIColor clearColor];
nameTextLabel.text = [channelList channelName];
nameTextLabel.font = [UIFont fontWithName:@"Arial" size:14];
[cell addSubview:nameTextLabel];
UILabel *genreTextLabel = [[UILabel alloc] initWithFrame: CGRectMake(80, 35, 175, 20)];
genreTextLabel.textColor = [UIColor grayColor];
genreTextLabel.backgroundColor = [UIColor clearColor];
NSString * str = [NSString stringWithFormat:@"%@ | %@",[channelList channelType],[channelList channelCounry]];
genreTextLabel.text = str;
genreTextLabel.font = [UIFont fontWithName:@"Arial" size:14];
[cell addSubview:genreTextLabel];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
return cell;
}
嘗試這種邏輯,可能會為您服務。
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier = [NSString stringWithFormat:@"Cell%d%d",indexPath.section,indexPath.row]; /// set different idenfifier for all cell
UITableViewCell * cell = [atableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SimpleTableIdentifier] autorelease];
/// Add UIImageView and UILable here
}
}
我希望這對您有幫助。
您應該使用SDWebImage避免延遲表滾動。 它使用緩存,避免每次都請求圖像URL。
克里特島之間的UILabel
和UIImageview
if(cell == nil)
{
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
.
.
.
// create `UILabel` and `UIImageview` here add add it to cell.contentView
}
兩者都在cell.contentView
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.