Iphone 如何从自定义UITableview检索单元格选择数据

Iphone 如何从自定义UITableview检索单元格选择数据,iphone,ios,uitableview,uilabel,didselectrowatindexpath,Iphone,Ios,Uitableview,Uilabel,Didselectrowatindexpath,可能是个愚蠢的问题 我有一个UITableview,有多个单元格。在每个单元格中,我显示一些数据。我没有使用单元格的文本属性来显示数据。相反,我的单元格中有一个自定义标签,用于显示文本。 我的问题是: 当我点击单元格时,我需要从单元格中检索数据。我怎样才能做到这一点 if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:C

可能是个愚蠢的问题

我有一个UITableview,有多个单元格。在每个单元格中,我显示一些数据。我没有使用单元格的文本属性来显示数据。相反,我的单元格中有一个自定义标签,用于显示文本。 我的问题是: 当我点击单元格时,我需要从单元格中检索数据。我怎样才能做到这一点

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];


    UILabel *CellTextlabel = [[UILabel alloc] init];
    CellTextlabel.tag = 222;
    [CellTextlabel setFrame:CGRectMake(40, 5, 200, 20)];
    [cell.contentView addSubview:CellTextlabel];
    [CellTextlabel release];
  }


UILabel *editCellTextlabel = (UILabel *)[cell.contentView viewWithTag:222];
editCellTextlabel.font = [UIFont boldSystemFontOfSize:18];
editCellTextlabel.text = contact.lastName;

-tableView:didSelectRowAtIndexPath:
方法中,可以从tableView的数组中获取数据:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    id objectForCell = [self.myArray objectAtIndex:indexPath.row];
    //do what you want with the above data.
}

-tableView:didSelectRowAtIndexPath:
方法中,可以从tableView的数组中获取数据:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    id objectForCell = [self.myArray objectAtIndex:indexPath.row];
    //do what you want with the above data.
}

您可以在
didselectrowatinexpath:
中使用

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *myLabel = [cell.contentView viewWithTag:222];

但可能值得一问的是,为什么要添加子标签而不是使用
textlab
属性?您可以修改其框架、设置等,然后就不必担心标签,因为默认情况下,此属性在
UITableViewCell
中公开

您可以通过

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *myLabel = [cell.contentView viewWithTag:222];

但可能值得一问的是,为什么要添加子标签而不是使用
textlab
属性?您可以修改其框架、设置等,然后就不必担心标记,因为此属性默认情况下在didSelectRowAtIndexPath方法的
UITableViewCell
中公开,您可以按如下操作:

UITableViewCell *cell = (UITableViewCell *)[self.tableViecellForRowAtIndexPath:indexPath];         
UILabel *textLabel = (UILabel *)[cell viewWithTag:222];

现在,您可以使用textLabel检索单元格UILabel中的数据。text

在didSelectRowAtIndexPath方法中,您可以按如下操作:

UITableViewCell *cell = (UITableViewCell *)[self.tableViecellForRowAtIndexPath:indexPath];         
UILabel *textLabel = (UILabel *)[cell viewWithTag:222];

现在,您可以使用textLabel检索单元格UILabel中的数据。text

发布用于填充单元格的代码。发布用于填充单元格的代码。我只是遇到了一些问题,因为我的单元格中有更多的内容要显示。作为初学者,如果不添加更多标签和按钮,而不是使用cells text属性和accessoryview等来处理对齐部分,而不是使用viewWithTag,我将对UITableViewCell进行子类化,并通过属性公开数据,这将使代码更具可读性。如果您有多个标签,我同意@deanWombourne的观点,您应该对UITableViewCell进行子类化,并在其中添加所有属性。您可以在cells init方法中初始化,并在layoutsubviews中设置每个子视图的框架。我只是遇到了一些问题,因为我的单元格中有更多的内容要显示。作为初学者,如果不添加更多标签和按钮,而不是使用cells text属性和accessoryview等来处理对齐部分,而不是使用viewWithTag,我将对UITableViewCell进行子类化,并通过属性公开数据,这将使代码更具可读性。如果您有多个标签,我同意@deanWombourne的观点,您应该对UITableViewCell进行子类化,并在其中添加所有属性。可以在cells init方法中初始化,并在layoutSubviews中设置每个子视图的框架