2012-10-09 50 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"]; 
}