从其他TableViewCell标签中匹配TableViewCell的标签

时间:2017-01-27 07:34:16

标签: ios objective-c uitableview

我的问题很难为我定义,即使我理解我要做的事情,我也会尽力把这个问题写成可以理解的,如果我没有任何问题描述,请指导我。

问题是我在UITableView内有UITableViewCellUITableViews都有自定义UITableViewCell类。外部晚餐UITableView的数据源和代表属于同一类。我正在处理像这样的数据数组

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView==self.mainTableView) {
        return titleArray.count;
    }
    return timeArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    if (tableView==self.mainTableView) 
    { 
        MainTableViewCell *cell = [tableView dequeueReusableCellWitentifier:@"cell"]; 
        cell.label.text = [dataArray objectAtIndex:indexPath.row]; 
        return cell; 
    } 
    InnerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    cell.view.backgroundColor = [UIColor greenColor]; 
    return cell; 
}

我的自定义单元格说明

  1. 主要UITableViewCell cusotm类属性 - UILabel(例如。 myLabel),UITableView(内部)
  2. 内部UITableViewCell自定义类属性 - UIView(例如。 MyView的)
  3. 现在我想只在myCabel'与我的字符串对象数组匹配。例如。

    if([myLabel.text isEqualTostring:@"22"]){
    cell.myView.backgroundColor = [UIColor blackColor];
    

    }

    例如。如果我有5个视图和2个标签,现在在主UITableView中有2行,在UITableView内有5行,但每个视图都应在外部单元格中 只有它匹配myLabel文本。

    请不要考虑匹配数据的来源不是匹配数据的问题。

    作为最后的结论,我想为每个内部UITableView单元创建不同的行数。

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    if( tableView == self.mainTableView ) 
    { 
        MainTableViewCell *cell = [tableView dequeueReusableCellWitentifier:@"cell"]; 
        cell.label.text = [dataArray objectAtIndex:indexPath.row]; 
        return cell; 
    } 
    InnerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    MainTableViewCell *parentCell = (MainTableViewCell*)cell.superView; 
    NSIndexPath *parentIndexPath = [self.mainTableView indexPathForCell:parentCell];

    NSString *text = [dataArray objectAtIndex:parentIndexPath.row];
    if( [text isEqualTostring:@"22"] //your condition goes here
    {
        cell.view.backgroundColor = [UIColor blackColor]; 
    }
    else
    {
        cell.view.backgroundColor = [UIColor greenColor]; 
    }

    return cell; 
}