I want to change the size and color of the different row (cell) in specified section so can any one give me the suggestion how to manage it.. Actually i m using 4 section on same view and i want to change the cell color and cell size in section 4 for that i wrote the following code in "heightForRowAtIndexPath" method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0)
{
return 44;
}
else if(indexPath.section==2)
{
return 44;
}
else if(indexPath.section==1)
{
return 100;
}
else
{
if (indexPath.row==0)
{
label6.frame=CGRectMake(0, 0, 320, 44);
label6.backgroundColor=[UIColor yellowColor];
return 44;
}
else if(indexPath.row==1)
{
return 100;
label6.frame=CGRectMake(0.0, 0,120,100);
label6.backgroundColor=[UIColor purpleColor];
label7 .frame=CGRectMake(122, 0,200,100);
label7.backgroundColor=[UIColor purpleColor];
}
else
{
label6.frame=CGRectMake(0.0, 0,120,40);
label7 .frame=CGRectMake(122, 0,200,40); 开发者_开发问答
return 44;
}
}
}
Thanks & Regards, Priyanka.
Now from the above question as far as i understood you want to change the height and cell color for some sections that you are making in your code well try the below code
In order to change the height of a particular cell in your section copy+paste the height for row method of the table view which is a delegate protocol which you have already done in your code here's a view at my code
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case 0: if(indexPath.row ==2) { return 25; } break; case 1: if(indexPath.row ==2) { return 25; } break; } return tableView.rowHeight; }
and now in the next part what i have done is changed the cell color for some rows i hope the code is self explanatory. Select the cell for row at index path method and add this code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } if(indexPath.section==0) { cell.textLabel.text = [arr objectAtIndex:indexPath.row]; if(indexPath.row ==2) { //changing cell color cell.backgroundColor = [UIColor grayColor]; } } else if(indexPath.section ==1) { if(indexPath.row ==2) { //changing cell color cell.backgroundColor = [UIColor blueColor]; } cell.textLabel.text = [arr1 objectAtIndex:indexPath.row]; } return cell; }
Hope this helps.....
You cannot change the color of the cell in the tableView:heightForRowAtIndexPath:
method. You will need to do that in the tableView:cellForRowAtIndexPath:
method. You should however pass the custom height in this method. So it would become,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 1:
return 100;
case 3:
if (indexPath.row == 1) {
return 100;
}
default:
return 44;
}
}
You can make all your cell customization in
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
...
if ( indexPath.section == 3 ) {
switch ( indexPath.row ) {
case 0:
cell.label6.frame=CGRectMake(0, 0, 320, 44);
cell.label6.backgroundColor=[UIColor yellowColor];
break;
case 1:
cell.label6.frame=CGRectMake(0.0, 0,120,100);
cell.label6.backgroundColor=[UIColor purpleColor];
cell.label7.frame=CGRectMake(122, 0,200,100);
cell.label7.backgroundColor=[UIColor purpleColor];
break;
default:
cell.label6.frame=CGRectMake(0.0, 0,120,40);
cell.label7.frame=CGRectMake(122, 0,200,40);
break;
}
}
}
Assuming label6
and label7
are members of the cell
.
精彩评论