[英]insertRowAnimation for the first cell doesn't work when batch inserting in UITableView
[英]UITableView's cell doesn't work as setting
我將uitableview的單元格設置如下
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.row == 0) {
cell.backgroundColor = [UIColor redColor];
UIImageView *bigphoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
bigphoto.image = [UIImage imageNamed:@"bigphoto.png"];
[cell addSubview:bigphoto];
}
else {
cell.backgroundColor = [UIColor blackColor];
cell.myphoto.image = [UIImage imageNamed:@"myphoto.png"];
cell.phototime.text = @"2014-03-01";
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 320;
}
return 220;
}
我希望第一個單元格(indexPath.row = 0)與其他單元格不同,第一個單元格設置為bigphoto,其背景顏色為紅色(使用其UI設計),其他為myphoto.png,黑色背景(使用UI)設計詳情為DetailCell,但代碼運行錯誤,
第一個單元格(indexPath.row = 0)是正確的,但indexPath.row = 3或6或9 ..與indexPath.row = 0相同,不像indexPath.row = 1或2或4 ...
那我該怎么辦呢? 謝謝
這是因為第一個細胞被“重復使用”。 由於您將這些單元格用於兩個單獨的目的,因此在故事板中使用原型單元格僅用於第一行,然后以其他方式重用其余行的單元格將更加清晰。
正在重用該單元,您應該為具有不同表示的單元使用不同的單元標識符。 這應該工作:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier;
DetailCell *cell = nil;
if (indexPath.row == 0) {
cellIdentifier = @"Cell0";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor redColor];
UIImageView *bigphoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
bighoto.tag = 1;
[cell addSubview:bigphoto];
}
UIImageView *bigphoto = (UIImageView *)[cell viewWithTag:1];
bigphoto.image = [UIImage imageNamed:@"bigphoto.png"];
}
else {
cellIdentifier = @"Cell1";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor blackColor];
}
cell.myphoto.image = [UIImage imageNamed:@"myphoto.png"];
cell.phototime.text = @"2014-03-01";
}
return cell;
}
在您的情況下,最好的選擇是使用靜態tabelViewCells。 請檢查此鏈接 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.