table view ::无法删除自定义单元格中的自定义标签

时间:2011-12-29 05:08:36

标签: iphone objective-c xcode cocoa-touch uitableview

在我的iPhone应用中,

在表格视图中

我在一个单元格中有两个标签..

  1. textLabel是默认值。
  2. 自定义标签
  3. 数据从数组中删除很好......

    这是代码..

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        //Get the Log Id for the sections. From Section Array
        int logID=0;
        if(indexPath.row==0)
        {
            NSLog(@"Time Array %@",timeArray);
            logID=[[[sectionArray objectAtIndex:indexPath.section] valueForKey:@"logID"] intValue];
            NSPredicate *p=[NSPredicate predicateWithFormat:@"logID==%d",logID];
            fillRows=nil;
            fillRows= [[timeArray filteredArrayUsingPredicate:p] mutableCopy];
        }
    
    
    
        static NSString *CellIdentifier = @"Cell";
    
    
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    
        }
    
    
    
    //Show Current Time.
    //"If condition for not to go for Array Index Out of Bound".
    if(indexPath.row<[fillRows count])
    {
    //Log CurrentTime    
        cell.textLabel.text=[[fillRows objectAtIndex:indexPath.row] valueForKey:@"logCurrentTime"];
        [cell.textLabel setTextColor:[UIColor whiteColor]];
    //Log Duration.   
       UILabel *lblDuration=[[[UILabel alloc] initWithFrame:CGRectMake(110, 11, 60, 21)] autorelease];
       [lblDuration setTextColor:[UIColor whiteColor]];
            [lblDuration setBackgroundColor:[UIColor clearColor]];
            [lblDuration setFont:[UIFont boldSystemFontOfSize:18]];
       lblDuration.text=[[fillRows objectAtIndex:indexPath.row] valueForKey:@"logDuration"];
            [cell.contentView addSubview:lblDuration];
       }
    
        return cell;
    }
    

    提前致谢:)

3 个答案:

答案 0 :(得分:0)

答案就是这个...... 为什么从te表中删除的单元格的标签粘在单元格上......

看到我已经评论了两行代码......

where cell == nil ..

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        //Get the Log Id for the sections. From Section Array

        static NSString *CellIdentifier = @"Cell";



        **UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     //   if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

       // }**



    /if(indexPath.row<[fillRows count])
    {
    //Log CurrentTime    
        cell.textLabel.text=[[fillRows objectAtIndex:indexPath.row] valueForKey:@"logCurrentTime"];
        [cell.textLabel setTextColor:[UIColor whiteColor]];
    //Log Duration.   
       UILabel *lblDuration=[[[UILabel alloc] initWithFrame:CGRectMake(110, 11, 60, 21)] autorelease];
       [lblDuration setTextColor:[UIColor whiteColor]];
            [lblDuration setBackgroundColor:[UIColor clearColor]];
            [lblDuration setFont:[UIFont boldSystemFontOfSize:18]];
       lblDuration.text=[[fillRows objectAtIndex:indexPath.row] valueForKey:@"logDuration"];
            [cell.contentView addSubview:lblDuration];
       }

        return cell;
    }

答案 1 :(得分:-1)

  if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
   }
 if(indexPath.row<[fillRows count])
 {
        [lblDuration setTextColor:[UIColor whiteColor]];
        [lblDuration setBackgroundColor:[UIColor clearColor]];
        [lblDuration setFont:[UIFont boldSystemFontOfSize:18]];
        [cell.contentView addSubview:lblDuration];

 }

试试这个..

答案 2 :(得分:-1)

使用表的委托方法作为解决方案

  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if(editingStyle == UITableViewCellEditingStyleDelete) {[array removeObjectAtIndex:indexPath.row]; [tableview reloadData];}}

你得到你想要的东西。