【问题标题】:change the appearance of a single UITableViewCell based on the content根据内容改变单个 UITableViewCell 的外观
【发布时间】:2013-09-26 23:19:59
【问题描述】:

我对带有自定义 UITableViewCell 的 UITableView 有疑问。 该表由 NSArray 填充,如果此 NSArray 中的对象以 - 开头,我希望它改变它的外观。

问题在于以 - 开头的 UITableViewCell 发生了变化,而且还更改了其他不应该更改的单元格。

这是我的代码:

  //this is the way in which change the height of the cell if the object in the array begins with -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *try = [arrayTitleEs objectAtIndex:indexPath.row];

if ([[try substringToIndex:1]isEqualToString:@"-"]) {

    return 45;

}

else return 160;
}


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

  static NSString *CellIdentifier = @"CellCardioScheda";
  CardioSchedaCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

   cell.titleEs.text = [arrayTitoloEs objectAtIndex:indexPath.row];

  NSString *try = [arrayTitoloEs objectAtIndex:indexPath.row];

if ([[try substringToIndex:1]isEqualToString:@"-"]) {

    cell.titleEs.frame = CGRectMake(0, 0, cell.frame.size.width-15, cell.frame.size.height);
}


 return cell;
}

从图中可以看出,以单元格开头的单元格-最小,文本向左移动,在下一个单元格中是可以的,但是最后一个单元格中的文本被移动了,但不应该!

谢谢大家

【问题讨论】:

    标签: iphone ios objective-c uitableview


    【解决方案1】:

    cellForRowAtIndexPath 中,您可以创建两种不同类型的单元格,并根据您的需要返回其中一种:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    
        UITableViewCell *firstCell = [tableView dequeueReusableCellWithIdentifier:@"firstCellID"];
    
        if (firstCell == nil) {
            firstCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCellID"] autorelease];
        }
    
        // Set here firstCell
    
        UITableViewCell *secondCell = [tableView dequeueReusableCellWithIdentifier:@"secondCellID"];
    
        if (secondCell == nil) {
            secondCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"secondCellID"] autorelease];
        }
    
        // Set here secondCell
    
        if ([[try substringToIndex:1]isEqualToString:@"-"]) {
            return secondCell;
        } else {
            return firstCell;
        }
    
    }
    

    【讨论】:

    • 不要同时创建两个单元格,然后选择返回其中一个。仅根据条件创建并返回一个。
    【解决方案2】:

    问题出在以下几点:

    if ([[try substringToIndex:1]isEqualToString:@"-"]) {
        cell.titleEs.frame = CGRectMake(0, 0, cell.frame.size.width-15, cell.frame.size.height);
    }
    

    如果条件不满足,您需要else 来正确设置框架。细胞被重复使用。您对一个单元格所做的任何事情都必须对所有单元格进行。

    if ([[try substringToIndex:1]isEqualToString:@"-"]) {
        cell.titleEs.frame = CGRectMake(0, 0, cell.frame.size.width-15, cell.frame.size.height);
    } else {
        cell.titleEs.frame = CGRectMake(...); // whatever regular cells should be
    }
    

    顺便说一句 - 您可以将 [[try substringToIndex:1]isEqualToString:@"-"] 替换为 [try hasPrefix:@"-"]

    【讨论】:

    • 感谢您的提示,hasPrefix 更快。我通过创建两个 UITableViewCell 解决了它,但是我阅读了您的评论,我想知道什么是最好的解决方案(我没有尝试过您的代码,但我认为它是正确的)
    猜你喜欢
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    相关资源
    最近更新 更多