2013-09-26 27 views
1

我用自定义UITableViewCell有一个UITableView问题。 该表由一个NSArray填充,我希望如果此NSArray中的某个对象以 - 开头 - 改变其外观。根据内容更改单个UITableViewCell的外观

问题是,以 - 开头的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; 
} 

as you can see from the picture that begins with the cell - is smallest and the text is moved to the left, in the next cell is all right, but in the last cell text spostasto but it should not!

,你可以从与细胞开始看到图片 - 最小和文本移动到左侧,在接下来的细胞好的,但最后一个单元格中的文本已移动,但不应该!

感谢所有

回答

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; 
    } 

} 
+0

请勿同时创建两个单元格,然后选择返回其中一个单元格。只根据条件创建并返回一个。 – rmaddy

+0

感谢您的回答,此代码已解决我的问题 – Ilario

+0

欢迎您:) – Beppe

0

问题是与以下几点:

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 hasPrefix:@"-"]取代[[try substringToIndex:1]isEqualToString:@"-"]

+0

感谢您的提示,hasPrefix速度更快。 我解决了它通过创建两个UITableViewCell,但我读了你的评论,我想知道什么是最好的解决方案(我没有尝试过你的代码,但我认为是正确的) – Ilario

相关问题