开发者

Adding UIImageView or UIView to the cell of the grouped table

开发者 https://www.devze.com 2023-03-16 21:54 出处:网络
I am having a problem setting a UIImageView or UIView on the cell of the Grouped table onthe iPhone. For this I am using this code

I am having a problem setting a UIImageView or UIView on the cell of the Grouped table on the iPhone.

For this I am using this code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    NSArray *listData =[self.tableContents objectForKey:[self.sotreKeys objectAtIndex:[indexPath section]]];


    UITableViewCell * cell = [tableView 
                              dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

    if(cell == nil) {

        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UIT开发者_如何学PythonableViewCellStyleDefault 
                 reuseIdentifier:SimpleTableIdentifier] autorelease];


    }
    if(indexPath.section == 1 && indexPath.row ==1)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 14, 40, 40)];
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"Back.png",nil]];
    [cell.contentView addSubview:imageView];
    [imageView release];
    }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];

    return cell;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

But it is not placed on the cell . When ever I select the row it will display for that time only, what is the problem?

Does anyone have any idea what the problem is. I think the image is getting overwritten by the cell .

Thanks in advance


try making

cell.accessoryType  = UITableViewCellAccessoryNone;

It seems that you are adding image on accessory view.

If yes, you can add cell.accessoryView = imageView;


I do have some ideas of what might be happening.

You are adding an image view to the contentView then you are setting the text of the textLabel which is a part of the contentView and is probably sitting on top of the image. Try this and see if you can at least see your image on the left side.

cell.imageView.image = [UIImage imageNamed:@"Back.png"];

Now when you dequeue cells you need to remember that all the views you added last time you created the cell are still there, so as you scroll you will just be stacking back buttons on top of each other.

Also the following line [tableView deselectRowAtIndexPath:indexPath animated:YES]; is dead code because it occurs after a return statement. You might want to place that in the delegate call – tableView:willDisplayCell:forRowAtIndexPath:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    NSArray *listData =[self.tableContents objectForKey:[self.sotreKeys objectAtIndex:[indexPath section]]];


    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if(cell == nil) {

        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault 
                 reuseIdentifier:SimpleTableIdentifier] autorelease];


    }
    if(indexPath.section == 1 && indexPath.row ==1)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 14, 40, 40)];
        imageView.image = [UIImage imageNamed:@"Back.png"];
        [cell.contentView addSubview:imageView];
        [imageView release];
    }

    cell.textLabel.text = [listData objectAtIndex:indexPath.row];

    return cell;
}

Make sure that your image is named "Back.png" not "back.png" because the iOS on the phone is key sensitive.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号