在uitableview中隐藏/显示detailTextLabel

时间:2013-09-04 12:31:42

标签: ios objective-c uitableview expand

我试图隐藏我的detailTextLabel.cell,而我的tableview加载,代码,

cell.textLabel.text=[array objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];

cell.detailTextLabel.hidden = YES;

在选择行时,尝试在didSelectRowAtIndexPath中显示细节,正如预期的那样,在按下行时显示详细信息,一旦我停止按下它,详细信息就会消失,为什么会这样,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"indexpath is %d", indexPath.row);
selectedIndex = indexPath.row;
isSearching = YES;

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

cell.textLabel.text=[dealarray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];

cell.detailTextLabel.hidden = NO;


[self.tableview endUpdates];



}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching && indexPath.row == selectedIndex)
{

    return 77;

}
return 44;


}

编辑:

在.h文件中分配变量'a'并使用如下代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text=[dealarray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];
    NSLog(@"dealarray %@\n %@",dealarray,detailarray);

    if (a==-1) {
        cell.detailTextLabel.hidden = YES;

    }
    else if(a==indexPath.row)
    {
        cell.detailTextLabel.hidden = NO;
               }
    else cell.detailTextLabel.hidden = YES;

return cell;


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"indexpath is %d", indexPath.row);
selectedIndex = indexPath.row;
isSearching = YES;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

cell.textLabel.text=[dealarray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];
a=indexPath.row;
[tableview reloadData];

}

2 个答案:

答案 0 :(得分:2)

为此,您可以使用两种类型的单元格。

对于普通数据,请使用基本单元格。在选择,重新加载表或单元格时,仅对选定的行使用另一个detailLabel单元格。

它将解决您的问题。

由于

答案 1 :(得分:0)

如果我了解您的要求,您可以尝试以下代码来解决您的目的。

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

     Write necessary code here....
     */

    // -------------
    cell.textLabel.text=[array objectAtIndex:indexPath.row];
    if (![selectedRowIndexs containsObject:[NSNumber numberWithInt:indexPath.row]])
    {
         cell.detailTextLabel.hidden = YES;

    }
    else
    {
        cell.detailTextLabel.hidden = NO;

    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.detailTextLabel.text=[detailarray objectAtIndex:indexPath.row];
    cell.detailTextLabel.hidden = NO;
    if (![selectedRowIndexs containsObject:[NSNumber numberWithInt:indexPath.row]])
    {
        [selectedRowIndexs addObject:[NSNumber numberWithInt:indexPath.row]];
    }

}