正在为我的tableview单元格做一个自定义单元格,我在customcell类中编写一个条件,如果indexpath.row = 0则为单元格创建2个图像,否则创建3个图像,
我的自定义单元格类
if (appDelegate.rowIndex==0)
{
UIButton *lObjImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
[lObjImageButton setBackgroundColor:[UIColor greenColor]];
//lObjImageButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton];
UIButton *lObjImageButton1=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
[lObjImageButton1 setBackgroundColor:[UIColor redColor]];
//lObjImageButton1.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton1];
imageButtonArray = [[NSArray alloc] initWithObjects:lObjImageButton, lObjImageButton1, nil];
}
else
{
UIButton *lObjImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
[lObjImageButton setBackgroundColor:[UIColor greenColor]];
//lObjImageButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton];
UIButton *lObjImageButton1=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
[lObjImageButton1 setBackgroundColor:[UIColor redColor]];
//lObjImageButton1.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton1];
UIButton *lObjImageButton2=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton2.frame=CGRectMake(150*2+10+5, 5, 150-10, 170+10);
[lObjImageButton2 setBackgroundColor:[UIColor blueColor]];
//lObjImageButton1.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton2];
imageButtonArray = [[NSArray alloc] initWithObjects:lObjImageButton, lObjImageButton1,lObjImageButton2, nil];
}
这里的appdelegate.rowindex值在我的viewcontroller里面的cellforrowindex中设置为appdelegate.rowstauts = indexpath.row
我运行应用程序,项目按照我的意愿执行,第一个单元格2图像和剩余单元格3图像, 但是当我滚动条件不匹配并使somtime第6个单元格为2个图像并且sumtime 3个单元格为2个图像而我的第1个单元格变为3个图像..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
BSImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
appDelegate.rowIndex=indexPath.row;
NSLog(@"cell.index= %i",cell.index);
if (cell == nil) {
cell = [[BSImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
滚动问题就出现了。
答案 0 :(得分:0)
问题在于UITableView使这些单元格可以重用,以提高性能。
你必须在cellForRowAtIndexPath中创建这个条件。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* reusableCellIdentifier = @"";
if(indexPath.row == 0){
reusableCellIdentifier = @"firstCell";
}else{
reusableCellIdentifier = @"otherCells";
}
}
答案 1 :(得分:0)
使用polymorphism代替检查自定义单元格中的条件。
写两个类:
@interface KSRTwoButtonCell : UITableViewCell { ... }
@interface KSRThreeButtonCell : UITableViewCell { ... }
然后再做
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL wantThreeButtonCell = (indexPath.row == 0);
NSString *cellIdentifier;
if (wantThreeButtonCell) {
cellIdentifier = @"ThreeButtonCell";
}
else {
cellIdentifier = @"TwoButtonCell";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
if (wantThreeButtonCell) {
cell = [[KSRThreeButtonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
else {
cell = [[KSRTwoButtonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
}
return cell;
}
答案 2 :(得分:0)
定义cell.h文件中的所有按钮和所有属性。 用
替换cell.m文件代码 lObjImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
[lObjImageButton setBackgroundColor:[UIColor greenColor]];
[self addSubview:lObjImageButton];
lObjImageButton1=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
[lObjImageButton1 setBackgroundColor:[UIColor redColor]];
//lObjImageButton1.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton1];
lObjImageButton2=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton2.frame=CGRectMake(150*2+10+5, 5, 150-10, 170+10);
[lObjImageButton2 setBackgroundColor:[UIColor blueColor]];
//lObjImageButton1.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton2];
imageButtonArray = [[NSArray alloc] initWithObjects:lObjImageButton, lObjImageButton1,lObjImageButton2, nil];
2.在返回单元格之前,在表委托方法中添加以下内容;
if(indexPath.row==0){
cell.lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
cell.lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
cell.lObjImageButton2.hidden =TRUE;
}
else{
cell.lObjImageButton2.hidden =FALSE;
cell.lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
cell.lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
cell.lObjImageButton2.frame=CGRectMake(150*2+10+5, 5, 150-10, 170+10);
}
希望它会对你有所帮助。
答案 3 :(得分:0)
创建- (void)resetCell
方法,将单元格重置为初始状态,并创建- (void)configureImageView:(int)rowIndex
,在BSImageCell
- (void)configureImageView:(int)rowIndex
{
if (rowIndex==0)
{
UIButton *lObjImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
[lObjImageButton setBackgroundColor:[UIColor greenColor]];
//lObjImageButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton];
UIButton *lObjImageButton1=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
[lObjImageButton1 setBackgroundColor:[UIColor redColor]];
//lObjImageButton1.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton1];
imageButtonArray = [[NSArray alloc] initWithObjects:lObjImageButton, lObjImageButton1, nil];
}
else
{
UIButton *lObjImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
[lObjImageButton setBackgroundColor:[UIColor greenColor]];
//lObjImageButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton];
UIButton *lObjImageButton1=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
[lObjImageButton1 setBackgroundColor:[UIColor redColor]];
//lObjImageButton1.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton1];
UIButton *lObjImageButton2=[UIButton buttonWithType:UIButtonTypeCustom];
lObjImageButton2.frame=CGRectMake(150*2+10+5, 5, 150-10, 170+10);
[lObjImageButton2 setBackgroundColor:[UIColor blueColor]];
//lObjImageButton1.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:lObjImageButton2];
imageButtonArray = [[NSArray alloc] initWithObjects:lObjImageButton, lObjImageButton1,lObjImageButton2, nil];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
BSImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[BSImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else
{
[cell resetCell];
}
[cell configureImageView:indexPath];
return cell;
}