我在IB中创建了一个有2个UIButton
s的单元格,并将其子类化。如何在不重复使用的情况下使用它?(对于固定的小桌子)我试着做一些类似的事情:RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
但这不会显示我在UITableView
中的单元格,只是一个空白单元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Where we configure the cell in each row
id currentRaffle = [_winnings objectAtIndex:indexPath.row];
RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"raffleResCell"];
if (cell == nil) {
cell = [[RaffleResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"raffleResCell"];
}
return cell;
}
避免可重用性不是一个好的做法,我会说不要这样做
可重复使用性在中完成
RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"raffleResCell"];
删除该行,每次只调用alloc方法,而不使用检查循环
像
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
id currentRaffle = [_winnings objectAtIndex:indexPath.row];
cell = [[RaffleResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"raffleResCell"];
return cell;
}
您告诉您在IB中设置UITableViewCell,然后您需要获取Nib文件,然后将该文件用作
// Get nib file from mainBundle
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RaffleResultCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
RaffleResultCell *cell = (RaffleResultCell *)currentObject;
break;
}
}
现在为按钮设置任何文本并返回单元格