我使用tableView,手动缓存单元格,但是当我滚动时,我得到一点滞后。 这是我的代码:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
var realRow = indexPath.row / 2
if indexPath.row / 2 > self.SUGGESTED_USERS_RATE && suggestedUsers.count > 0
{
--realRow
}
if indexPath.row / 2 > self.SUGGESTED_RADIOS_RATE && suggestedRadioStations.count > 0
{
--realRow
}
if indexPath.row % 2 == 1
{
return self.CELL_MARGIN
}
else if indexPath.row / 2 == self.SUGGESTED_USERS_RATE && suggestedUsers.count > 0
{
return self.SUGGESTED_USERS_CELL_HEIGHT
}
else if indexPath.row / 2 == self.SUGGESTED_RADIOS_RATE && suggestedRadioStations.count > 0
{
return self.SUGGESTED_USERS_CELL_HEIGHT
}
else
{
return self.CELLS_HEIGHT[realRow]//[indexPath.row / 2 - indexPath.row / (self.SUGGESTED_USERS_RATE * 2 + 2)]
}
//让row = indexPath.row / 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var realRow = indexPath.row / 2
if indexPath.row / 2 > self.SUGGESTED_USERS_RATE && suggestedUsers.count > 0
{
--realRow
}
if indexPath.row / 2 > self.SUGGESTED_RADIOS_RATE && suggestedRadioStations.count > 0
{
--realRow
}
if indexPath.row % 2 == 1
{
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "")
cell.backgroundColor = UIColor.grayColor().colorWithAlphaComponent(0.3)
cell.selectionStyle = .None
addSeperatorsToView(cell.contentView, width: self.bounds.width,
height: self.CELL_MARGIN, up: true, down: true, left: false, right: false)
return cell
}
else if indexPath.row / 2 == self.SUGGESTED_RADIOS_RATE && suggestedRadioStations.count > 0
{
let cell = SuggestedUsersCell(width: self.bounds.width, height: self.SUGGESTED_USERS_CELL_HEIGHT, radios: suggestedRadioStations.prefix(5) + [])
return cell
}
else if indexPath.row / 2 == self.SUGGESTED_USERS_RATE && suggestedUsers.count > 0
{
let cell = SuggestedUsersCell(width: self.bounds.width, height: self.SUGGESTED_USERS_CELL_HEIGHT, users: suggestedUsers.prefix(5) + [])
return cell
}
else
{
var cell : ActivityTableViewCell
if let myCell = self.cells_cache[realRow]
{
cell = myCell
}
else
{
let activity = self.actevetiesArr[realRow]
cell = ActivityTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: self.CELL_ID,
height: self.CELLS_HEIGHT[realRow],
width: self.bounds.width, activity: activity ,
index : realRow ,
delegate : self)
self.cells_cache[realRow] = cell
}
cell.userInteractionEnabled = true
cell.selectionStyle = .None
return cell
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
var count = self.actevetiesArr.count * 2
if self.actevetiesArr.count > self.SUGGESTED_USERS_RATE
{
count = count + 2
}
if self.actevetiesArr.count > self.SUGGESTED_RADIOS_RATE
{
count = count + 2
}
if self.actevetiesArr.count < max(self.SUGGESTED_USERS_RATE , self.SUGGESTED_RADIOS_RATE)
{
count += 2
self.SUGGESTED_RADIOS_RATE = 1
self.SUGGESTED_USERS_RATE = 0
}
else
{
self.SUGGESTED_USERS_RATE = SUGGESTED_USERS_INDEX
self.SUGGESTED_RADIOS_RATE = SUGGESTED_RADIOS_INDEX
}
if suggestedRadioStations.count == 0
{
count -= 2
}
if suggestedUsers.count == 0
{
count -= 2
}
return count//(self.actevetiesArr.count + (self.actevetiesArr.count
}
有什么问题??! ,我没有在cellForRowAtIndexPath中做大量的代码。
注意:cell_cache是一个字典==&gt; [Int:ActivityTableViewCell
答案 0 :(得分:2)
问题是你没有使用惯用法 - 即你应该使用可重复使用的单元格。
答案 1 :(得分:1)
添加此
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 44;
}
或者您可以添加
self.tableView. estimatedRowHeight = 44.0
中的 viewDidLoad
让我知道它是否有效
答案 2 :(得分:1)
尝试用户可重复使用的单元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = (CustomCell *) [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}