iphone - 如何清除uitableviewcell

标签 iphone ios uitableview

您好,我有一个自定义 uitableview 单元格部分,当用户选择它们时,我从 subview 中分配值。

自定义单元格由两个文本标签组成,一个是标题,另一个是从 subview 中选择的元素将被分配的位置(虽然它没有被分配,但它被分配了字符串“empty”。

我希望可以选择滑动单个单元格,然后如果用户已为其分配值,则允许用户将 uilabel 的内容清除回“空”。

我已经尝试这样做,并且已经让它工作到一定程度,但是我遇到了 uitableview 变得无响应的问题,直到用户再次触摸 tableview .. 然后它像正常一样工作。

这是我的实现,我想知道这是否是唯一的方法,或者它们是否是更好的方法,因为一旦我使用了滑动,我就会遇到表格响应的问题。

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"Clear";
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {

       //Clears the correct uitableviewcell and releases the correct nsstring in the view 
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                //reset uitableviewcell textlabel with default input "empty"
            vehicleSearchCell = [tableView cellForRowAtIndexPath:indexPath];
            UILabel *label2;
            label2 = (UILabel *)[vehicleSearchCell viewWithTag:2];
            label2.text = @"empty";
            [tableView reloadData]; //may or may not need to call this now
            }
            else if (indexPath.row == 1) {
                //reset uitableviewcell textlabel with default input "empty"
            }
            else if (indexPath.row == 2) {
                //reset uitableviewcell textlabel with default input "empty"
            }
            else (indexPath.row == 3) {
                //reset uitableviewcell textlabel with default input "empty"

            }

        } 
    }
}

future 更新。 我还有另一个用于按钮的自定义单元格(自定义单元格中有一个按钮)并且用户也可以滑动这个(但是我不想要)清除按钮也显示在此但是在清除按钮之后已经使用过(显然什么都不加)你可以第一次按下另一个单元格,没有延迟..但是如果你要用第 0 节中的任何单元格来做,那么你会没有反应...... enter image description here

更新 我开始考虑这是我使用标签等加载单元格的方式。你们认为这可能是导致问题的原因吗?如果是这样,我该如何解决这个问题..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"VehicleSearchCell" owner:self options:nil];
        cell = vehicleSearchCell;
        self.vehicleSearchCell = nil;
    }
    // Configure the cell...
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            UILabel *label1;
            label1 = (UILabel *)[cell viewWithTag:1];
            label1.text = @"Manufacture";

            UILabel *label2;
            label2 = (UILabel *)[cell viewWithTag:2];
            label2.text = manufactureSearchObjectString;
        }
        else if(indexPath.row == 1)
        {
            [[NSBundle mainBundle] loadNibNamed:@"VehicleSearchCellDual" owner:self options:nil];
            cell = vehicleSearchCellDual;
            self.vehicleSearchCellDual = nil;

            UILabel *label;
            label = (UILabel *)[cell viewWithTag:1];
            label.text = @"Model";

            UILabel *label2;
            label2 = (UILabel *)[cell viewWithTag:2];
            label2.text = @"empty";

            UILabel *label3;
            label3 = (UILabel *)[cell viewWithTag:3];
            label3.text = @"empty";
        }
        else if(indexPath.row == 2)
        {
            UILabel *label;
            label = (UILabel *)[cell viewWithTag:1];
            label.text = @"Year";

            UILabel *label2;
            label2 = (UILabel *)[cell viewWithTag:2];
            label2.text = @"empty";
        }
        else if(indexPath.row == 3)
        {
            UILabel *label;
            label = (UILabel *)[cell viewWithTag:1];
            label.text = @"Key type";

            UILabel *label2;
            label2 = (UILabel *)[cell viewWithTag:2];
            label2.text = keyTypeSearchObjectString;
        }
    }
    else if(indexPath.section == 1)
    {
        if(indexPath.row == 0)
        {
            [[NSBundle mainBundle] loadNibNamed:@"VehicleSearchButtonCell" owner:self options:nil];
            cell = searchButtonCell;
            self.searchButtonCell = nil;
        }
    }
    return cell;
}

最佳答案

我对您的代码中的某些内容感到有些困惑和担心

    manufactureSearchObjectString = @"empty"; //Is manufactureSearchObjectString a member variable?
    [self.tableView reloadData]; //reloads the tabels so you can see the value.
    [manufactureSearchObjectString release]; //Why do you release this here?? This will leak/throw a wobbly more than likely.

如果 manufactureSearchObjectString 是你的类的成员变量,我假设你已经在某个时候分配了它。在将它指向其他地方之前,您可能希望先释放它。于是上面的代码就变成了

 [manufactureSearchObjectString release];
 manufactureSearchObjectString = [[NSString alloc] initWithString:@"empty"];
 [self.tableView reloadData]; //reloads the tabels so you can see the value.
         

如果您之前没有在其他任何地方分配它,那么根本不需要发布。

您可能还想研究使用 switch 语句,甚至使用“else if's”而不是普通的 if 语句作为

    if (indexPath.row == 0) //if row is 0 test is correct
    {
    }
    if (indexPath.row == 1) //if row is 0 and passed the first test this still gets tested here
    {
    }
    if (indexPath.row == 2) //same again row gets tested again when it's already passed the == 0 test 

将此更改为

if (indexPath.row == 0) //if row is 0 test is correct
{
}
else if (indexPath.row == 1) //if row is 0 it's not so will not get tested again
{
}
else if (indexPath.row == 2)
{}
else //remember to finish with a plain else

在大多数情况下,第二个选项将比第一个运行得更快,因为 == 测试只会在成功之前发生。当然,这需要的时间取决于表格中的行数和部分数。

但是,如果您想要更通用的方法,您可以调用 cellForRowAtIndexPath: 并直接访问那里的字符串。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
       CustomUITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
       [cell setSecondCustomTextField:@"empty"]; //or could be cell.secondCustomTextField = @empty";
       [tableView reloadData]; //may or may not need to call this now
    }
}

我假设您已经为自定义表格 View 单元格中的可编辑文本字段设置了一个 setter。如果没有..你应该做一个。

编辑:

好的,你打电话

[[NSBundle mainBundle] loadNibNamed:@"VehicleSearchCellDual" owner:self options:nil];

但没有引用它到任何东西,你不需要这样做。我认为你正试图从它的 NIB 加载你的自定义 UITableViewCell,但你正在以错误的方式解决这个问题。我建议您阅读 this Coco with Love 关于自定义表格 View 单元格的帖子。
我觉得将两个文本字段添加到 UITableViewCell 中,自定义 NIB 有点矫枉过正,但是由于您选择这样做,我将以这种方式回答您的问题。 首先,当每个自定义单元格都如此相似时,拥有三个自定义单元格并没有多大意义。您可以只创建一个自定义 UITableViewCell Nib 文件(这也会使您的代码更易于阅读),因此我们将您的 nib 文件用于“VehicleSearchCellDual”,因为它具有我们所追求的大部分功能,请忽略其他两个“VehicleSearchCell”和“VehicleSearchButtonCell”,因为您不需要它们。现在从“VehicleSearchButtonCell”中复制 UIButton 并将其插入到“VehicleSearchCellDual”中并将其设置为隐藏在界面生成器中,同时将标签值设置为 4。只要您在界面生成器中设置标签以匹配你在下面调用的值然后调用 viewWithTag: 应该返回你之后的内容,现在看到我们将使用你的“VehicleSearchCellDual”Nib 我会假设标签 1 对应于最左边的文本字段,标签 2是中央文本字段,标签 3 是最右边的文本字段。 好的,现在您已经完成了,您可以按以下方式加载表格单元格。

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
   NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"VehicleSearchCellDual" owner:self options:nil];
   for( NSObject* nibItem in nib )
   {
        if( [nibItem isKindOfClass:[UITableViewCell class]] )
        {
            cell = (UITableViewCell*)nibItem;
            break;
        }
    }
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if(indexPath.section == 0)
{
    UILabel* titleLabel;
    titleLabel = (UILabel *)[cell viewWithTag:1];
    
    UILabel* rightMostLabel;
    rightMostLabel = (UILabel *)[cell viewWithTag:3];

    if(indexPath.row == 0)
    {
        [titleLabel setText:@"Manufacture"];
        [rightMostLabel setText:manufactureSearchObjectString];
    }
    else if(indexPath.row == 1)
    {
        [titleLabel setText:@"Model"];
        [rightMostLabel setText:@"empty"];
        UILabel* leftMostLabel;
        leftMostLabel = (UILabel *)[cell viewWithTag:2];
        leftMostLabel.text = @"empty"; 
    }
    else if(indexPath.row == 2)
    {
        [titleLabel setText:@"Year"];
        [rightMostLabel setText:@"empty"];
    }
    else if(indexPath.row == 3)
    {
        [titleLabel setText:@"Key type"];
        [rightMostLabel setText:keyTypeSearchObjectString];
    }
}
else if(indexPath.section == 1)
{
    if(indexPath.row == 0)
    {
        [[cell viewWithTag:1] setHidden:YES];
        [[cell viewWithTag:2] setHidden:YES];
        [[cell viewWithTag:3] setHidden:YES];
        [[cell viewWithTag:4] setHidden:NO];
    }
}
return cell;

现在,在将 rightMostLabel 和 leftMostLabel 设置为 @"empty"的上方,您可能希望将它们设置为适合该字段的任何成员变量“searchString”。否则每次重新加载表时,这些字段值将设置为“空”。 这应该会让您走上正确的轨道。

关于iphone - 如何清除uitableviewcell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7654507/

相关文章:

iphone - 如何处理动态 UITableView 中对 UITextFields 的引用 (iPhone)

ios - NSURL 扩展采用 StringLiteralConvertible

ios - AlertController 在保存时不保存 textField 文本,仅编辑

objective-c - Objective-C : How to highlight a tableView Cell and keep it highlighted when table is reloaded?

ios - 向左滑动时 iOS 7 上的 TableView 崩溃

iphone - 如何根据标题文本增加 UIButton 高度?

iphone - RestKit:使用命名路由的 POST 请求

ios - UIWebview全屏尺寸

ios - 在单元格的 imageView 中为图像添加边框

ios - swift ,使用 alamofire 将图像上传到 php 服务器