向上和向下滚动时单元格子视图重复

时间:2014-09-14 13:15:09

标签: ios tableview

我有一个UITableView,我正在以编程方式使用它。在其cellforrowatindexpath我正在做以下事情,但细胞上没有任何内容。同样在didselecrow我打算在单元格中添加一个标签,但是我每次向上和向下滚动时都会在另一个单元格上复制标签。这是代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Fire Cell for Row Podcast");

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    //Title
    LabelTitle = [UILabel new];
    LabelTitle.tag = 1;
    LabelTitle.textColor = [UIColor colorWithRed:42/255.0f green:54/255.0f blue:97/255.0f alpha:1];
    NSString *strTitle = [NSString stringWithFormat:@"%@",[content.podcastTitle objectAtIndex:indexPath.row]];
    [DynamicUI DynamicLabel:LabelTitle textInput:strTitle fontContent:[UIFont fontWithName:@"DroidArabicNaskh-Bold" size:12] originX:15 originY:30 originW:295];
    [cell.contentView addSubview:LabelTitle];

    // Date
    LabelDate = [UILabel new];
    LabelTitle.tag = 2;
    LabelDate.textColor = [UIColor colorWithRed:164/255.0f green:153/255.0f blue:57/255.0f alpha:1];
    [cell.contentView addSubview:LabelDate];
    NSString *strDate = [NSString stringWithFormat:@"%@",[content.podcastDate objectAtIndex:indexPath.row]];
    [DynamicUI DynamicLabel:LabelDate textInput:strDate fontContent:[UIFont fontWithName:@"Droid Arabic Naskh" size:10.5] originX:15 originY:10 originW:275];

}

LabelTitle = (UILabel *) [cell.contentView viewWithTag:1];
LabelDate = (UILabel *) [cell.contentView viewWithTag:2];
calendarImage = (UIImageView *) [cell.contentView viewWithTag:3];

// Title
NSString *strTitle = [NSString stringWithFormat:@"%@",[content.podcastTitle objectAtIndex:indexPath.row]];
//[DynamicUI DynamicLabel:LabelTitle textInput:strTitle fontContent:[UIFont fontWithName:@"DroidArabicNaskh-Bold" size:12] originX:15 originY:30 originW:295];
    LabelTitle.text = strTitle;

// Date
NSString *strDate = [NSString stringWithFormat:@"%@",[content.podcastDate objectAtIndex:indexPath.row]];
//[DynamicUI DynamicLabel:LabelDate textInput:strDate fontContent:[UIFont fontWithName:@"Droid Arabic Naskh" size:10.5] originX:15 originY:10 originW:275];
    LabelDate.text = strDate;

return cell;
}

和didselectrow代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
UILabel *downloadingLabel  = [[UILabel alloc]initWithFrame:CGRectMake(300, 10, 100, 20)];
downloadingLabel.text = @"Downloading";
downloadingLabel.backgroundColor = [UIColor greenColor];
[[self.tableView cellForRowAtIndexPath:indexPath].contentView addSubview:downloadingLabel];
}

0 个答案:

没有答案