我有一个 UIViewController 使用 UITableView 来显示一个数组。这个数组从 Core Data 中获取数据。除了一次显示超过 8 个条目之外,所有的工作都很好,当滚动表格 View 时,行会出现在其他行之上。行中的文本也周期性地开始变得有些模糊。很奇怪。
这是我的“rowForCell”方法的代码。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Creates a new cell for every row (as in, every time this method is called).
//UITableViewCell *aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//'alloc init' is NOT called here therefore a cell object has to be made inside the table view in the storyboard (which I've done and named cell). This is a more effecient and better way then 'alloc init'. Cells are dequeued & reused.
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (aCell == nil)
{
aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
}
//Makes sure the separator doesn't have that little gap in the beginning.
//NOTE: This MUST be here and "_tableView.separatorInset = UIEdgeInsetsZero;" must exist inside [self extraTableViewParameter]!
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
aCell.layoutMargins = UIEdgeInsetsZero;
aCell.preservesSuperviewLayoutMargins = NO;
}
//Cell details.
aCell.backgroundColor = [UIColor clearColor];
aCell.textLabel.textColor = [UIColor whiteColor];
//aCell.textLabel.font = [UIFont fontWithName:@"SanFrancisco" size:20];
aCell.detailTextLabel.textColor = [UIColor lightGrayColor];
//aCell.detailTextLabel.font = [UIFont fontWithName:@"SanFrancisco" size:20];
//Fill cell with data.
//NSDictionary *item = [_tableData objectAtIndex:indexPath.row]; //[_fetchArray objectAtIndex:indexPath.row];
//AssetsPartners *items = [_fetchArray objectAtIndex:indexPath.row]; //Using this line of code, instead of the bottom one, will result in _fetchArray NOT being sorted.
AssetsPartners *items = [_fetchedResultsController objectAtIndexPath:indexPath]; //Ask '_fetchedResultsController' what's at this particular indexPath right now. Also alphabetically sorts the array list.
//CUSTOM LABEL instead of standard '.textLabel.text' because 'items.name' is sometimes too long & covers up price: looks messy.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(65.0, 15.0, 150.0, 30.0)];
label.textColor = [UIColor whiteColor];
label.text = items.name;
[aCell.contentView addSubview:label];
aCell.textLabel.text = @""; //Make sure this is empty!
//aCell.textLabel.text = items.name; //item[@"title"]; //Preiously, was just this with out the UILabel.
NSInteger anInteger = [items.balance integerValue];
aCell.detailTextLabel.text = [NSString stringWithFormat:@"%@ P", [self addWhitespacesTo:anInteger]]; //item[@"balance"]];
//Check if it's appropriate to add image to cell or not.
if (items.photo == nil)
{
NSLog(@"No image.");
}
else
{
NSString *iconName = [self cutOutString:@"bizzer://" fromString:items.photo]; //Get rid of "bizzer://" from string value.
//Standard UITableViewCell accessors/parameters. Only used to create a clear image to shift text over.
aCell.imageView.image = [UIImage imageNamed:iconName]; //Inputs appropriate icon into cell image.
[self turnIcon:aCell.imageView toColor:@"clearColor"]; //Changes icons color accordingly.
//Entirely new UIImageView for every UITableViewCell (to tweak the frame size & position).
UIImageView *newImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 15.0, 35.0, 35.0)];
newImgView.image = [UIImage imageNamed:iconName];
[aCell.contentView addSubview:newImgView];
[self turnIcon:newImgView toColor:items.color];
}
return aCell;
}
最佳答案
尝试重新排列您的代码如下
if (aCell == nil)
{
aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(65.0, 15.0, 150.0, 30.0)];
label.textColor = [UIColor whiteColor];
label.tag = 999;
[aCell.contentView addSubview:label];
}
UILabel *label = (UILabel *)[aCell.contentView viewWithTag:999];
AssetsPartners *items = [_fetchedResultsController objectAtIndexPath:indexPath];
label.text = items.name;
关于ios - 如果行数超过 8 行,部分自定义 UITableViewCell 会很有趣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33741956/