table view :: could not delete the custom label in custom cell

时光总嘲笑我的痴心妄想 提交于 2019-12-25 01:33:18

问题


In my iPhone app,

In Table view

I have Two labels in one cell..

  1. textLabel which is default.
  2. Custom Label

Data is deleting from array which is fine...

Here is the code..

- (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;
}

Thanks in advance :)


回答1:


Answer is this... that why the label of the cell which was deleted from te table stuck to the cell...

Just see I have commented two lines of codes......

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;
    }



回答2:


  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];

 }

Try this ..




回答3:


use the delegate method of table for your solution

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

u get what u want.



来源:https://stackoverflow.com/questions/8664212/table-view-could-not-delete-the-custom-label-in-custom-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!