UITableView常用操作

1、UITableViewCell的默認高度是44;

2、iPhone提供了4種基本的表格視圖單元格,在SDK 3.0 之後,每個單元格都有3個屬性textLabel,detailTextLabel和imageView。

下面一一介紹這4種基本格式:

(1)UITableViewCellStyleDefault

該格式提供了一個簡單的左對齊的文本標籤textLabel和一個可選的圖像imageView。如果顯示圖像,那麼圖像將在最左邊。

這種格式雖然可以設置detailTextLabel,但是不會顯示該標籤。

(2)UITableViewCellStyleSubtitle

該格式與前一種相比,增加了對detailTextLabel的支持,該標籤將會顯示在textLabel標籤的下面,字體相對較小。

(3)UITableViewCellStyleValue1

該格式居左顯示textLabel,居右顯示detailTextLabel,且字體較小。

該格式不支持圖像。

(4)UITableViewCellStyleValue2

該格式居左現實一個小型藍色主標籤textLabel,在其右邊顯示一個小型黑色副標題詳細標籤detailTextLabel。

該格式不支持圖像

3、UITableViewStylePlain分割線會有多餘的空白分割線,去除多餘分割線方式可以添加footer實現:

UIView *view =[ [UIViewalloc]init]; 
view.
backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:view];
[view release];

4、當UITableView的類型是UITableViewStyleGrouped時,他的backgroundColor沒有意義,因爲有個backgroundView屬性,所以可以這樣做:

self.view.backgroundColor  = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"mainBg"]];

tv=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,320,480)style:UITableViewStyleGrouped];

tv.backgroundView =nil;
 tv.backgroundColor = [UIColorclearColor];

5、UITableViewCell在多選刪除時需要注意(全選cell和反選時用):

self.tableview.allowsMultipleSelectionDuringEditing =YES;   // 允許多選

下面是全選時最該注意的地方,千萬別用[otherCell setSelected:YES animated:YES];或者otherCell.selected去設置選中狀態。

if (allSelectFlag ==0) {

        for (NSIndexPath *cellIndexin [self indexPathForTableView:tableview])

        {

            [self.tableviewselectRowAtIndexPath:cellIndex animated:YESscrollPosition:UITableViewScrollPositionBottom];

        }

        allSelectFlag = 1;

    }else{

        for (NSIndexPath *cellIndexin [self indexPathForTableView:tableview])

        {

            [self.tableviewdeselectRowAtIndexPath:cellIndex animated:YES];

        }

        allSelectFlag = 0;

    }

/**

 * 返回單sectiontableview中的所有的IndexPath

 */

-(NSArray *)indexPathForTableView:(UITableView *)tableView

{

    NSInteger rows =  [tableView numberOfRowsInSection:0];

    NSMutableArray *indexPaths = [NSMutableArrayarray];

    for (int row = 0; row < rows; row++) {

        NSIndexPath *indexPath = [NSIndexPathindexPathForRow:row inSection:0];

        [indexPaths addObject:indexPath];

    }

    return indexPaths;

}


7、cell在重用的時上下滑動cell內容很可能會亂,特別是針對某些行填充的內容不同的情況。可以做以下處理解決:

if (cell == nil)

    {

        cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier]autorelease];            

    }else{           // 當重用cell時清空所有cell內容,重新寫入。

        while ([cell.contentView.subviewslastObject]!=nil) {

            [(UIView*)[cell.contentView.subviewslastObject] removeFromSuperview];

        }

    }

8. UITableView在創建之後就會調用dataSource代理方法,即使你沒有調用 [self.view addSubview : myTableView ]; 那些dataSource代理方法都會被調用,當執行了addSubview後dataSource代理方法會從新調用。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章