0

我试图在我的表格视图加载时隐藏我的 detailTextLabel.cell,通过代码,

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

cell.detailTextLabel.hidden = YES;

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

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

}

4

2 回答 2

2

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

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

它会解决你的问题。

谢谢

于 2013-09-04T12:39:04.723 回答
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]];
    }

}
于 2013-09-05T09:53:09.753 回答