纯代码手写的tableview怎么可重用cell,代码如下,求大神帮助,真心哭了 #pragma 表格行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FromID = @"ChatFromCell";
static NSString *ToID = @"ChatToCell";
XMPPMessageArchiving_Message_CoreDataObject *message = [_fetchedResultsController objectAtIndexPath:indexPath];
ChatMessageCell *cell = nil;
if (message.isOutgoing) {
cell = [tableView dequeueReusableCellWithIdentifier:FromID];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:ToID];
}
// 设置单元格
// [cell.messageButton setTitle:message.body forState:UIControlStateNormal];
[cell setMessage:message.body isOutgoing:message.isOutgoing];
if (message.isOutgoing) {
cell.headImageView.image = self.myImage;
} else {
cell.headImageView.image = self.bareImage;
}
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FromID = @"ChatFromCell";
static NSString *ToID = @"ChatToCell";
XMPPMessageArchiving_Message_CoreDataObject *message = [_fetchedResultsController objectAtIndexPath:indexPath];
ChatMessageCell *cell = nil;
if (message.isOutgoing) {
cell = [tableView dequeueReusableCellWithIdentifier:FromID];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:ToID];
}
// 设置单元格
// [cell.messageButton setTitle:message.body forState:UIControlStateNormal];
[cell setMessage:message.body isOutgoing:message.isOutgoing];
if (message.isOutgoing) {
cell.headImageView.image = self.myImage;
} else {
cell.headImageView.image = self.bareImage;
}
return cell;
}
if (message.isOutgoing) {
cell = [tableView dequeueReusableCellWithIdentifier:FromID];
if (cell == nil) {
cell = ...
}
} else {
cell = [tableView dequeueReusableCellWithIdentifier:ToID];
if (cell == nil) {
cell = ...
}
}
这是从重用队列中去取cell , uitableview第一次加载cell时,重用队列中还未有可重用的cell ,所以通过这段代码返回的cell会是nil
如楼上的代码, 需要添加上当cell从重用队列获取失败时的处理,也就是需要重新创建cell的实例。如果数据结构和布局都一样的话,可以使用一个重用的identifier.