2012-10-09 53 views
1

我創建了自定義UItableViewCell。我在故事板中的單元格中添加了一個標籤,並與課程連接。我想根據行索引更改標籤的位置。這是我的代碼:自定義UITableViewCell不響應更改

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

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

// Configure the cell... 

cell.titleLabel.frame = CGRectMake(100.0, 20.0, cell.titleLabel.frame.size.width, cell.titleLabel.frame.size.height); 

cell.titleLabel.text = @"Test"; 

return cell; 

}

的問題是,標籤不改變位置,其在同一個地方,我把它在故事板。但是,當我上下滾動時,單元格會再次加載,標籤將移動到正確的位置。我究竟做錯了什麼?

+0

呃,如果你連接IBOutlet它會工作。這個方法對我來說聽起來很新穎dequeueReusableCellWithIdentifier:forIndexPath:?爲什麼索引路徑?另外,你是否試圖在自定義單元格中實現prepareForReuse:? – Leonardo

+0

獅子座,這是一個常用的方法來使可重複使用的單元出列:)但是當你第一次加載表時,沒有單元出列,所以它似乎是錯誤的。 – Stas

回答

0

看來你實際上忘了創建一個單元格(至少我沒有看到創建單元格的代碼)。這可能看起來像這樣:

SPAccountHeaderCell * headerCell = (SPAccountHeaderCell *)[tableView dequeueReusableCellWithIdentifier:@"headerCell"]; 
if (headerCell == nil) { 
    headerCell = [[SPAccountHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"headerCell"]; 
}