开发者

iPhone: Use UITableViewCellStyleValue1 and UITableViewCellStyleValue2 in same tableView

What is the best way to use both UITableViewCellStyleValue1 and UITableViewCellStyleValue2 in same tableView? I am not sure the best way to do this, since I initWithStyle before my sw开发者_运维技巧itch:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

    }

    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = @"Username";

                break;
            case 1: 
                cell.textLabel.text = @"Password";
                break;
        }//end switch

    } else if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0: 
                cell.textLabel.text = @"Create Account";

                break;
        }//end switch
    }

    return cell;

}//end

How can I use UITableViewCellStyleValue2 in my "Create Account" cell?


Why don't you move initWithStyle inside your switch? If you're trying to save some room and you need Value2 rare comparing to Value1, then you can leave initWithStyle before switch, and then create it again inside switch for cells of Value2 style.


Determine the cell style and identifier before calling initWithStyle.

You need to use a different cell re-use identifier for each style because if a cell with style Value1 gets de-queued, you don't want to re-use it for a row that needs style Value2.

static NSString *CellIdentifierValue1 = @"CellIdentifierValue1";
static NSString *CellIdentifierValue2 = @"CellIdentifierValue2";

//default settings...
NSString *reuseIdentifier = CellIdentifierValue1;
UITableViewCellStyle cellStyle = UITableViewCellStyleValue1;

if ((indexPath.section == 1) && (indexPath.row == 0))
{
    //special settings for CreateAccount...
    reuseIdentifier = CellIdentifierValue2;
    cellStyle = UITableViewCellStyleValue2;
}

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:cellStyle reuseIdentifier:reuseIdentifier] autorelease];
}

//rest of existing code...
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜