我有以下代码可在应用程序中创建视图。但是,当我滚动时,底部将被第1节项目(一个包含图像的单元格)代替,有什么建议吗?我已经尝试了很多方法(特别是使用dequeueReusableCellWithIdentifier进行操作),但是没有任何效果。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
simpleCell *cell = (simpleCell *)[self.tableview dequeueReusableCellWithIdentifier:@"simple" forIndexPath:indexPath];
cell.imageView.image = nil;
cell.text.text = nil;
//cell = [[simpleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"simple"];
if(indexPath.row == 0){
cell.text.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"isFacebookLoggegIn"]){
cell.imageView.image = [UIImage imageNamed:@"fb_tbl_connect.png"];
}else{
cell.imageView.image = [UIImage imageNamed:@"tbl_disconnect_fb.png"];
}
}
if(indexPath.row == 1){
cell.text.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];
cell.text.text = @"Edit Your Profile";
}
return cell;
}
if (indexPath.section == 1) {
simpleCell *cell = (simpleCell *)[tableView dequeueReusableCellWithIdentifier:@"simple" forIndexPath:indexPath];
if(indexPath.row == 0){
cell.text.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];
cell.text.text = @"Send Feedback";
}
return cell;
}
if (indexPath.section == 2) {
simpleCell *cell = (simpleCell *)[tableView dequeueReusableCellWithIdentifier:@"simple" forIndexPath:indexPath];
if (indexPath.row == 0) {
cell.text.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];
cell.text.text = @"Visit Wowpads.com";
}
if (indexPath.row == 1) {
cell.text.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];
cell.text.text = @"Terms of Service";
}
}
return 0;
}
}
您的花括号未对齐。用于
的结尾括号if (indexPath.section == 0) {
在方法的末尾,而不是在设置节0的布局的代码结尾之后。这导致节1和2的项目设置不正确。
在重新使用单元格的情况下,设置单元格中的所有值也是一种好习惯。即在第1和2节中设置单元时,请设置cell.imageView.image = nil
。
我认为您的代码有误,可以在开始时设置单元格属性,然后在每种情况下设置值。试试这个,希望它能解决您的问题。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
simpleCell *cell = (simpleCell *)[self.tableview dequeueReusableCellWithIdentifier:@"simple" forIndexPath:indexPath];
if (!cell) {
cell.text.font = [UIFont fontWithName:@"ProximaNova-Semibold" size:13];
}
cell.text = @"";
cell.imageView.image = nil;
if (indexPath.section == 0) {
if(indexPath.row == 0){
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"isFacebookLoggegIn"])
{
cell.imageView.image = [UIImage imageNamed:@"fb_tbl_connect.png"];
} else {
cell.imageView.image = [UIImage imageNamed:@"tbl_disconnect_fb.png"];
}
}
if(indexPath.row == 1){
cell.text.text = @"Edit Your Profile";
}
}
if (indexPath.section == 1) {
if(indexPath.row == 0){
cell.text.text = @"Send Feedback";
}
}
if (indexPath.section == 2) {
if (indexPath.row == 0) {
cell.text.text = @"Visit Wowpads.com";
}
if (indexPath.row == 1) {
cell.text.text = @"Terms of Service";
}
}
return cell;
}
在CellForRowAtIndexpath中添加
如果(单元格){
cell=nil;
[cell removeFromSuperview];
}
并且还要检查heightforRowAtIndexpath之类的-(叶酸)至-(CGFloat)
@ NigelG是的..它工作正常。我在cellForRowAtIndexPath中添加了这些行。
if(indexPath.row == 0){
[cell.img setImage:[UIImage imageNamed:@"img1.png"]];
}
else{
cell.img.image = nil;
[cell.img setImage:[UIImage imageNamed:@"img2.png"]];
}