2014-03-19 77 views
0

我的cell.textLabel.text有時會返回空字符串。有沒有辦法可以跳過這個單元格,並在我的最後一個非空cell.textLabel.text後面放上我的下一個文本標籤?所以不應該有空標籤。如何跳過UITableViewCells中的空標籤

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:MyIdentifier]; 
    } 
    ResultModel *resultModel = [self.array objectAtIndex:indexPath.row]; 
    cell.textLabel.text = resultModel.resultString; 

    return cell; 
} 

回答

0

cell.textLabel需要的全部內容視圖的size.width所以你計算字符串的長度和操縱下一個標籤相應

0

試試這個代碼。

if([resultModel.resultString isEqualToString:@""]) 
{ 
    // Display the label 
cell.textLabel.text = resultModel.resultString; 
} 
else 
{ 
    //result string is Null do nothing here. 
} 
0

爲什麼你不只是得到在「resultString」屬性中有價值的實例?

NSArray *dataWithResultString = [call the custom method to get instances with no empty value]; 
ResultModel *resultModel = [dataWithResultString objectAtIndex:indexPath.row]; 
0

這個你應該在數據源陣列本身在處理......

填充與具有resultModel.resultString值的新數組並使用此數組作爲數據源的表

0

如果我沒有理解您正確地希望只有在resultModel.resultString中存在非空字符串時才顯示單元格,對嗎?

爲此,您最好處理數據源數組本身。如果這很難,你可以做到這一點。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
ResultModel *resultModel = [self.array objectAtIndex:indexPath.row]; 
if([resultModel.resultString isEqualToString:@""]) 
return 0.0; 
else 
return 50;//or whatever 
} 

但它更好地處理數據源本身。

0

如果我正在讀你的話,結果數組可能會返回一個空字符串,如果是的話,你不想顯示這些行的權利?

理想情況下,您應該爲查詢添加另一個參數排除空的resultString。但是,如果您想要hacky方式,請聲明(int)padRows並將其設置爲0 in viewDidLoad;

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
static NSString *MyIdentifier = @"MyIdentifier"; 

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:MyIdentifier]; 
} 
ResultModel *resultModel = [self.array objectAtIndex:indexPath.row]; 

while([resultModel.resultString isEqualToString:@""]) { 
    _padRows += 1; 
    resultModel = [self.array objectAtIndex:(indexPath.row+_padRows)]; 
} 

cell.textLabel.text = resultModel.resultString; 

return cell; 
} 

代碼未測試。